标签:public get timeout resource ret pool 管理 long oid
1. 在配置文件中添加 注解 <cache:annotation-driven cache-manager="cacheManager" key-generator="keyGenerator" />
2.定义缓存管理侧率
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<ref bean="sharedCache1" />
<ref bean="sharedCache2" />
</set>
</property>
</bean>
3 实际的缓存处理类
<bean id="sharedCache1" class="cache.redis.DataCache">
<constructor-arg name="name" value="default" />
<constructor-arg name="cacheService" ref="cacheService" />
</bean>
<bean id="sharedCache2" class="cache.redis.DataCache">
<constructor-arg name="name" value="cache2s" />
<constructor-arg name="cacheService" ref="cacheService" />
<property name="expire" value="2" />
</bean>
4. 配置jedisPoolConfig
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.parseInt(cachebundle.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.parseInt(cachebundle.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.parseLong(cachebundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.parseBoolean(cachebundle.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.parseBoolean(cachebundle.getString("redis.pool.testOnReturn")));
5 生成一个jedisPool
JedisPool pool4master = new JedisPool(config, cachebundle.getString("cache.redis.host"), Integer.parseInt(cachebundle.getString("cache.redis.port")), Integer.parseInt(cachebundle.getString("redis.pool.timeout")));
6.得到一个jedis连接
jedis = pools.get(MASTER_KEY_PREFIX).getResource();
7.释放一个jedis连接
pools.get(MASTER_KEY_PREFIX).returnBrokenResource(jedis);
8.设置redis主库值
public static void set(Object key, Object value, int expire) {
Jedis redis = getRedisConnection();
byte[] skey = SerializationUtils.serialize(toSerializable(key));
byte[] svalue = SerializationUtils.serialize(toSerializable(value));
redis.set(skey, svalue);
redis.expire(skey, expire);
returnResource(redis);
}
9.从redis丛库得到一个值
public static Object get(Object key) {
Jedis redis = getSlaveRedisConnection();
byte[] result = redis.get(SerializationUtils.serialize(toSerializable(key)));
returnSlaveResource(redis);
return result == null ? null : SerializationUtils.deserialize(result);
}
标签:public get timeout resource ret pool 管理 long oid
原文地址:http://www.cnblogs.com/suixin84/p/6700441.html