spring 、Redis缓存配置
102niu
9年前
来自: http://my.oschina.net/u/2317118/blog/607397
在spring配置文件中加入如下配置:
<!-- redis --> <bean id="redisDataSource" class="redis.clients.jedis.JedisPoolConfig" p:maxIdle="300" p:maxWaitMillis="1000" p:testOnBorrow="true" p:maxTotal="600"> </bean> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pwd}" p:pool-config-ref="redisDataSource" /> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:keySerializer-ref="stringRedisSerializer" p:valueSerializer-ref="jdkSerializationRedisSerializer" p:connectionFactory-ref="redisConnectionFactory" />
java代码中的使用:
import org.springframework.data.redis.core.RedisTemplate; public class RedisService { @Resource(name = "redisTemplate") private RedisTemplate<String, Object> redisTemplate; /** * 保存数据 * * @param key * @param obj * @return false :失败,true:成功 */ public boolean insertObject(final String key, final Object obj) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); if (null != connection.get(keys)) { return false; } byte[] values = serializer.serialize(toJson(obj)); return connection.setNX(keys, values); } }); } /** * 修改数据 * * @param key * @param obj * @return false :失败,true:成功 */ public boolean updateObject(final String key, final Object obj) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); if (null != connection.get(keys)) { if (1 == connection.del(keys)) { byte[] values = serializer.serialize(toJson(obj)); return connection.setNX(keys, values); } return false; } return false; } }); } /** * 删除数据 * * @param key * @return false :失败,true:成功 */ public boolean deleteObject(final String key) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); if (1 == connection.del(keys)) { return true; } return false; } }); } /** * 按Key查询 * @param key * @return */ private String queryDataFromRedis(final String key) { return redisTemplate.execute(new RedisCallback<String>() { public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keys = serializer.serialize(key); byte[] values = connection.get(keys); if (values == null) { return null; } return serializer.deserialize(values); } }); } private RedisSerializer<String> getRedisSerializer() { return redisTemplate.getStringSerializer(); } }