Spring+Memcached整合

本文介绍如何将Memcached与Spring框架整合使用,包括直接初始化MemcachedClient的方法及通过封装一层简化使用的实践。文中提供了详细的配置文件及测试代码。

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

上篇博文简单介绍了Memcached的安装,与使用,现在介绍一下Spring与Memcached整合,都比较简单,可以在spring中直接初始化MemcachedClient,也可以重新封装一层,封装一个Memcached类。

这里写图片描述

1.直接初始化MemcachedClient
properties资源文件:

memcached.servers=127.0.0.1:11211
memcached.initConn=10
memcached.minConn=10
memcached.maxConn=1024
memcached.maxIdle=300000
memcached.poolName=memcached

spring配置文件:

<?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"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-2.5.xsd" >
    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" 
      init-method="initialize">
        <constructor-arg>  
            <value>memcachedPool</value>  
        </constructor-arg>  
        <property name="initConn" value="${memcached.initConn}" />
        <property name="minConn" value="${memcached.minConn}" />
        <property name="maxConn" value="${memcached.maxConn}" />
        <property name="maxIdle" value="${memcached.maxIdle}" />
        <property name="servers" value="${memcached.servers}" />
    </bean>

     <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">  
        <constructor-arg>  
            <value>memcachedPool</value>  
        </constructor-arg>  

    </bean>  
</beans>

测试代码:

package com.test.memcached;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.danga.MemCached.MemCachedClient;
public class Main {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
    MemCachedClient  cache=(MemCachedClient)ctx.getBean("memcachedClient");
    cache.set("memcache_spring", "memcache_spring_value");
    System.out.println(cache.get("memcache_spring"));

}
}

2.封装一层使用
在实际项目中,封装一层,对外使用还是比较好的。在我实际接触的项目中,就是按照这样的方式:
spring配置文件

<?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"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-2.5.xsd" >

<!--
     <context:annotation-config />
     <context:component-scan base-package="com.test.memcached"/>-->



     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations">
            <list>
                <value>classpath:memcached.properties</value>
            </list>
        </property>
     </bean>
    <!--1:调用了init方法在Memcached里初始化-->
    <!--
    <bean id="memcached" class="com.test.memcached.Memcached"  init-method="init">
        <property name="poolName" value="memcached" />
        <property name="initConn" value="10" />
        <property name="minConn" value="10" />
        <property name="maxConn" value="1024" />
        <property name="maxIdle" value="300000" />
        <property name="servers" value="127.0.0.1:11211" />
    </bean>-->
    <!--2:可以通过memcached.propeties读取,用上面明确指定也可以-->
    <bean id="memcached" class="com.test.memcached.Memcached"  init-method="init">
        <property name="poolName" value="${memcached.poolName}" />
        <property name="initConn" value="${memcached.initConn}" />
        <property name="minConn" value="${memcached.minConn}" />
        <property name="maxConn" value="${memcached.maxConn}" />
        <property name="maxIdle" value="${memcached.maxIdle}" />
        <property name="servers" value="${memcached.servers}" />
    </bean>
</beans>

封装后的Memcached类:

package com.test.memcached;
import java.util.Date;
import java.util.Map;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class Memcached {

    private MemCachedClient mc;
    private String poolName;
    private String[] servers = {};
    private int initConn = 8;
    private int minConn = 8;
    private int maxConn = 64;

    private long maxIdle = 1000 * 60 * 5;
    public void init() {
        SockIOPool pool = SockIOPool.getInstance(poolName);
        pool.setServers(servers);
        pool.setInitConn(initConn);
        pool.setMinConn(minConn);
        pool.setMaxConn(maxConn);
        pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
        pool.initialize();
        mc = new MemCachedClient(poolName);

    }

    public void shutDown() {
        SockIOPool pool = SockIOPool.getInstance(poolName);
        if (pool != null && pool.isInitialized())
            pool.shutDown();
    }

    /*删除key的缓存*/
    public boolean delete(String key) {
        return mc.delete(key, null, null);
    }

    public boolean delete(String key, Date expiry) {
        return mc.delete(key, null, expiry);
    }

    public boolean delete(String key, Integer hashCode, Date expiry) {
        return mc.delete(key, hashCode, expiry);
    }

    public boolean set(String key, Object value) {
        return mc.set(key, value);
    }

    public boolean set(String key, Object value, Integer hashCode) {
        return mc.set(key, value, hashCode);
    }

    public boolean set(String key, Object value, Date expiry) {
        return mc.set(key, value, expiry);
    }

    public boolean set(String key, Object value, Date expiry, Integer hashCode) {
        return mc.set(key, value, expiry, hashCode);
    }

    public Object get(String key) {
        return mc.get(key);
    }

    public Object[] get(String[] keys) {
        return mc.getMultiArray(keys);
    }

    public boolean flushAll() {
        return mc.flushAll();
    }

    public boolean flushAll(String[] keys) {
        return mc.flushAll(keys);
    }

    public String getPoolName() {
        return poolName;
    }

    public void setPoolName(String poolName) {
        this.poolName = poolName;
    }

    public String[] getServers() {
        return servers;
    }

    public void setServers(String[] servers) {
        this.servers = servers;
    }

    public int getInitConn() {
        return initConn;
    }

    public void setInitConn(int initConn) {
        this.initConn = initConn;
    }

    public int getMinConn() {
        return minConn;
    }

    public void setMinConn(int minConn) {
        this.minConn = minConn;
    }

    public int getMaxConn() {
        return maxConn;
    }

    public void setMaxConn(int maxConn) {
        this.maxConn = maxConn;
    }

    public long getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(long maxIdle) {
        this.maxIdle = maxIdle;
    }
}

测试代码:

package com.test.memcached;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.danga.MemCached.MemCachedClient;
public class Main {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
/*  MemCachedClient  cache=(MemCachedClient)ctx.getBean("memcachedClient");
    cache.set("memcache_spring", "memcache_spring_value");
    System.out.println(cache.get("memcache_spring"));*/
    Memcached memcached=(Memcached)ctx.getBean("memcached");
    memcached.set("memcache_spring_3", "memcache_spring_value_3");
    System.out.println(memcached.get("memcache_spring_3"));

}
}

上面的代码,就简单测试一下存取,都比较简单,就没有测试其他方法了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值