Skip to content

Commit

Permalink
add Spring code and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimone committed Nov 19, 2011
1 parent b6ddd27 commit 05ea40a
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 35 deletions.
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

88 changes: 88 additions & 0 deletions README.md
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)
46 changes: 17 additions & 29 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,24 @@
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.7.3</version>
</dependency>

<!-- Only necessary if using Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>


<build>
<plugins>
<!-- The maven app assembler plugin will generate a script that sets up the classpath and runs your project -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.heroku.devcenter.Main</mainClass>
<name>memcache</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
5 changes: 0 additions & 5 deletions src/main/java/com/heroku/devcenter/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public static void main(String[] args) throws IOException, URISyntaxException {
ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder();
ConnectionFactory cf = factoryBuilder.setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build();

// URI base = new URI("http://" + System.getenv("MEMCACHE_SERVERS") + ":11211");
// ArrayList<URI> baseURIs = new ArrayList<URI>();
// baseURIs.add(base);
// MemcachedClient memcachedClient = new MemcachedClient(baseURIs, "default", System.getenv("MEMCACHE_USERNAME"), System.getenv("MEMCACHE_PASSWORD"));

MemcachedClient memcachedClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress(System.getenv("MEMCACHE_SERVERS"), 11211)));
memcachedClient.add("test", 0, "testData");
System.out.println(memcachedClient.get("test"));
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/heroku/devcenter/spring/Main.java
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 src/main/java/com/heroku/devcenter/spring/MemcacheConfig.java
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 src/main/java/com/heroku/devcenter/spring/SpringConfig.java
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;
}
}
18 changes: 18 additions & 0 deletions src/main/resources/applicationContext.xml
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>

0 comments on commit 05ea40a

Please sign in to comment.