memcached整合项目

1.传统xml注入

创建spring-memcached文件

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

    <!-- 添加属性文件-->
    <context:property-placeholder location="classpath*:memcache.properties"  file-encoding="UTF-8"/>
    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
          init-method="initialize"    destroy-method="shutDown">
        <property name="servers">
            <list>
                <value>${memcache.server1}</value>
                <value>${memcache.server2}</value>
            </list>
        </property>
        <property name="weights">
            <list>
                <value>${memcache.weights1}</value>
                <value>${memcache.weights2}</value>
            </list>
        </property>
        <property name="initConn">
            <value>${memcache.initConn}</value>
        </property>
        <property name="minConn">
            <value>${memcache.minConn}</value>
        </property>
        <property name="maxConn">
            <value>${memcache.maxConn}</value>
        </property>
        <property name="maintSleep">
            <value>${memcache.maintSleep}</value>
        </property>
        <property name="nagle">
            <value>${memcache.nagle}</value>
        </property>
        <property name="socketTO">
            <value>${memcache.socketTO}</value>
        </property>
    </bean>
    <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
    </bean>
</beans>

创建memcache.properties文件

memcache.server1=192.168.1.110:11211
memcache.server2=192.168.1.111:11211
memcache.weights1=3
memcache.weights2=5
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000

备注:xml形式注入方法网上好多,就不过多说明。因为楼主最近使用springboot框架,它的最大特点就是遵循“习惯优于配置”的原则,使用Spring Boot只需要很少的配置,大部分的时候我们直接使用默认的配置即可 。官方文档中也告诉我们尽量减少xml文件的使用,所有我们使用另一种方式注入形式。

2.java bean注入形式

创建application.properties文件

#缓存机制配置
memcache.servers=192.168.1.110:11211
memcache.weights=5
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000

创建SockIOPoolConfig类

/**
 * 自定义类,读取配置文件中memcache的内容
 * Created by zhang on 2017/3/21.
 */
@Component
@ConfigurationProperties(prefix = "memcache",locations = "classpath:application.properties")
public class SockIOPoolConfig {

    private String[] servers;

    private Integer[] weights;

    private int initConn;

    private int minConn;

    private int maxConn;

    private long maintSleep;

    private boolean nagle;

    private int socketTO;

    set/get方法省略
}

Spring Boot 使用一些松的规则来绑定属性到@ConfigurationProperties bean 并且支持分层结构(hierarchical structure)。
该注解的主要目的就是读取配置文件。其中,prefix 属性是配置文件的前缀,locations是对应的配置文件的路径。如果你的配置内容放到application.properties文件时,locations可省略。

创建MemcachedConfig类

/**
 * 配置连接池(创建SockIOPool、MemCachedClient 对象)
 * Created by zhang on 2017/3/13.
 */
@Component
public  class MemcachedConfig {
    @Autowired
    SockIOPoolConfig sockIOPoolConfig;
    @Bean
    public SockIOPool sockIOPool(){
        //获取连接池的实例
        SockIOPool pool = SockIOPool.getInstance();
        //服务器列表及其权重
        String[] servers = sockIOPoolConfig.getServers();
        Integer[] weights = sockIOPoolConfig.getWeights();
        //设置服务器信息
        pool.setServers(servers);
        pool.setWeights(weights);
        //设置初始连接数、最小连接数、最大连接数、最大处理时间
        pool.setInitConn(sockIOPoolConfig.getInitConn());
        pool.setMinConn(sockIOPoolConfig.getMinConn());
        pool.setMaxConn(sockIOPoolConfig.getMaxConn());
        //设置连接池守护线程的睡眠时间
        pool.setMaintSleep(sockIOPoolConfig.getMaintSleep());
        //设置TCP参数,连接超时
        pool.setNagle(sockIOPoolConfig.isNagle());
        pool.setSocketConnectTO(sockIOPoolConfig.getSocketTO());
        //初始化并启动连接池
        pool.initialize();
        return pool;
    }

    @Bean
    @ConditionalOnBean(SockIOPool.class)
    public MemCachedClient memCachedClient(){
        return new MemCachedClient();
    }
}

spring Boot的强大之处在于使用了Spring 4框架的新特性:@Conditional注释,此注释使得只有在特定条件满足时才启用一些配置。@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)

创建CacheController测试类

@ApiIgnore
@RestController
@RequestMapping(value="/demo")
public class CacheController {
    @Autowired
    MemCachedClient memCachedClient;

    @RequestMapping("/cacheSave")
    public String cacheSave(@RequestParam(value="id", required=false, defaultValue="228")String id)
     {
        boolean i = memCachedClient.set("id", id, 1000);
        return String.valueOf(i);
    }
}

测试结果

true
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值