Spring 集成 MemCache

本文介绍了如何通过XML配置文件设置Memcached客户端连接池,并在Java中使用@Autowired注解注入MemcachedClient对象进行缓存操作。同时提供了pom.xml依赖管理和Memcached客户端源码及下载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 下载地址:

https://github.com/gwhalin/Memcached-Java-Client/downloads

转载于:https://www.cnblogs.com/zno2/p/4538737.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值