memcached整合项目

    xiaoxiao2021-03-25  8

    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
    转载请注明原文地址: https://ju.6miu.com/read-114245.html

    最新回复(0)