Spring 集成 MemCache
1)xml
<bean class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> <constructor-arg> <value>someSockIOPool</value> </constructor-arg> <property name="servers"> <list> <value>someIP1:11261</value> <value>someIP2:11261</value> </list> </property> </bean> <bean class="com.danga.MemCached.MemCachedClient"> <!-- 需要与SockIOPool同名,不写时为默认值default ,可自行查看源码 --> <constructor-arg> <value>someSockIOPool</value> </constructor-arg> </bean>
2)java
@Autowired MemCachedClient memCachedClient; @Test public void memcacheTest() { memCachedClient.add("kk", "1"); System.out.println(memCachedClient.get("kk")); }
3)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.test</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>test</name> <repositories> <repository> <id>nexus</id> <name>local private nexus</name> <url>http://maven.oschina.net/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>com.danga</groupId> <artifactId>java-memcached</artifactId> <version>2.6.3</version> </dependency> </dependencies> </project>
java-memcached 源码:
https://github.com/gwhalin/Memcached-Java-Client/
java-memcached 下载地址: