forked from heroku/devcenter-memcache-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
208 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
## Using Memcache from Java | ||
|
||
Spymemcached is a popular Java Memcache client. In order to use Spymemcached in your project you have to declare the dependency in your build and initialize the client from the environment variables that Heroku provides to your application. | ||
|
||
### Add Spymemcached to Your Pom.xml | ||
|
||
Add the following repository and dependency to your pom.xml in order to use Spymemcached to connect to Memcache: | ||
|
||
<repository> | ||
<id>spy</id> | ||
<name>Spy Repository</name> | ||
<layout>default</layout> | ||
<url>http://files.couchbase.com/maven2/</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</repository> | ||
|
||
... | ||
|
||
<dependency> | ||
<groupId>spy</groupId> | ||
<artifactId>spymemcached</artifactId> | ||
<version>2.7.3</version> | ||
</dependency> | ||
|
||
### Use Spymemcached in Your Application | ||
|
||
:::java | ||
AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(System.getenv("MEMCACHE_USERNAME"), System.getenv("MEMCACHE_PASSWORD"))); | ||
ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder(); | ||
ConnectionFactory cf = factoryBuilder.setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(); | ||
|
||
MemcachedClient memcachedClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress(System.getenv("MEMCACHE_SERVERS"), 11211))); | ||
memcachedClient.add("test", 0, "testData"); | ||
|
||
### Using Spymemcached with Spring | ||
|
||
When using Spymemcached with Spring you can create a bean that will hold your Memcache configuration and then use Spring to initialize that bean: | ||
|
||
Memcache Configuration Bean: | ||
|
||
public class MemcacheConfig { | ||
private String servers; | ||
private String username; | ||
private String password; | ||
|
||
//getters and setters ommitted | ||
} | ||
|
||
This bean can be initialized with either Java or XML based spring configuration: | ||
|
||
Java Configuration: | ||
|
||
:::java | ||
@Configuration | ||
public class SpringConfig { | ||
@Bean | ||
public MemcacheConfig getMemcachedConfig() { | ||
MemcacheConfig mcConfig = new MemcacheConfig(); | ||
mcConfig.setServers(System.getenv("MEMCACHE_SERVERS")); | ||
mcConfig.setUsername(System.getenv("MEMCACHE_USERNAME")); | ||
mcConfig.setPassword(System.getenv("MEMCACHE_PASSWORD")); | ||
return mcConfig; | ||
} | ||
} | ||
|
||
or XML Configuration: | ||
|
||
<bean class="com.heroku.devcenter.spring.MemcacheConfig"> | ||
<property name="servers" value="#{systemEnvironment['MEMCACHE_SERVERS'] }"/> | ||
<property name="username" value="#{systemEnvironment['MEMCACHE_USERNAME'] }"/> | ||
<property name="password" value="#{systemEnvironment['MEMCACHE_PASSWORD'] }"/> | ||
</bean> | ||
|
||
Client Creation: | ||
|
||
:::java | ||
// For xml based config use GenericXmlApplicationContext | ||
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); | ||
MemcacheConfig config = ctx.getBean(MemcacheConfig.class); | ||
AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(config.getUsername(), config.getPassword())); | ||
ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder(); | ||
ConnectionFactory cf = factoryBuilder.setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(); | ||
MemcachedClient memcachedClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress(config.getServers(), 11211))); | ||
|
||
You can also download the [sample code](http://github.com/heroku/devcenter-memcache-java.git) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.heroku.devcenter.spring; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.URISyntaxException; | ||
import java.util.Collections; | ||
|
||
import net.spy.memcached.ConnectionFactory; | ||
import net.spy.memcached.ConnectionFactoryBuilder; | ||
import net.spy.memcached.ConnectionFactoryBuilder.Protocol; | ||
import net.spy.memcached.MemcachedClient; | ||
import net.spy.memcached.auth.AuthDescriptor; | ||
import net.spy.memcached.auth.PlainCallbackHandler; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.support.GenericXmlApplicationContext; | ||
|
||
public class Main { | ||
|
||
/** | ||
* @param args | ||
* @throws IOException | ||
* @throws URISyntaxException | ||
*/ | ||
public static void main(String[] args) throws IOException, URISyntaxException { | ||
//ApplicationContext ctx = new GenericXmlApplicationContext("applicationContext.xml"); | ||
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); | ||
MemcacheConfig config = ctx.getBean(MemcacheConfig.class); | ||
|
||
AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, | ||
new PlainCallbackHandler(config.getUsername(), config.getPassword())); | ||
ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder(); | ||
ConnectionFactory cf = factoryBuilder.setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(); | ||
|
||
MemcachedClient memcachedClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress(config.getServers(), 11211))); | ||
memcachedClient.add("testSpring", 0, "testDataSpring"); | ||
System.out.println(memcachedClient.get("testSpring")); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/heroku/devcenter/spring/MemcacheConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.heroku.devcenter.spring; | ||
|
||
public class MemcacheConfig { | ||
|
||
private String servers; | ||
private String username; | ||
private String password; | ||
|
||
public String getServers() { | ||
return servers; | ||
} | ||
public void setServers(String servers) { | ||
this.servers = servers; | ||
} | ||
public String getUsername() { | ||
return username; | ||
} | ||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
public String getPassword() { | ||
return password; | ||
} | ||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/heroku/devcenter/spring/SpringConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.heroku.devcenter.spring; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SpringConfig { | ||
|
||
@Bean | ||
public MemcacheConfig getMemcachedConfig() { | ||
MemcacheConfig mcConfig = new MemcacheConfig(); | ||
mcConfig.setServers(System.getenv("MEMCACHE_SERVERS")); | ||
mcConfig.setUsername(System.getenv("MEMCACHE_USERNAME")); | ||
mcConfig.setPassword(System.getenv("MEMCACHE_PASSWORD")); | ||
return mcConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | ||
http://www.springframework.org/schema/context | ||
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> | ||
|
||
<context:annotation-config/> | ||
|
||
<bean class="com.heroku.devcenter.spring.MemcacheConfig"> | ||
<property name="servers" value="#{systemEnvironment['MEMCACHE_SERVERS'] }"/> | ||
<property name="username" value="#{systemEnvironment['MEMCACHE_USERNAME'] }"/> | ||
<property name="password" value="#{systemEnvironment['MEMCACHE_PASSWORD'] }"/> | ||
</bean> | ||
|
||
</beans> |