本篇文章主要介绍了"redis 与 Spring 集成",主要涉及到方面的内容,对于企业开发感兴趣的同学可以参考一下:
ApplicationContent-reids.xml配置:
<...
ApplicationContent-reids.xml配置:
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:c
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
package com.sc.ew.redis.dao.source;
import redis.clients.jedis.ShardedJedis;
public interface RedisDataSource {
/**
* 取得redis的客户端,可以执行命令了。
* @return
*/
public abstract ShardedJedis getRedisClient();
public void returnResource(ShardedJedis shardedJedis);// 将资源返还给pool
public void returnResource(ShardedJedis shardedJedis, boolean broken);// 出现异常后,将资源返还给pool (其实不需要第二个方法)
}
package com.sc.ew.redis.dao.source.impl;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import com.sc.ew.redis.dao.source.RedisDataSource;
@Repository
public class RedisDataSourceImpl implements RedisDataSource {
private static final Logger logger = Logger
.getLogger(RedisDataSourceImpl.class);
@Resource
private ShardedJedisPool shardedJedisPool;
public ShardedJedis getRedisClient() {
try {
ShardedJedis shardJedis = shardedJedisPool.getResource();
return shardJedis;
} catch (Exception e) {
logger.error("getRedisClent error", e);
}
return null;
}
public void returnResource(ShardedJedis shardedJedis) {
shardedJedisPool.returnResource(shardedJedis);
}
public void returnResource(ShardedJedis shardedJedis, boolean broken) {
if (broken) {
shardedJedisPool.returnBrokenResource(shardedJedis);
} else {
shardedJedisPool.returnResource(shardedJedis);
}
}
}
package com.sc.ew.redis.template;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.sc.ew.redis.dao.source.RedisDataSource;
import com.sc.ew.redis.util.SerializationUtils;
import redis.clients.jedis.ShardedJedis;
@Repository
public class RedisHashTemplate {
private static final Logger logger = Logger
.getLogger(RedisHashTemplate.class);
@Autowired
private RedisDataSource redisDataSource;
public void disconnect() {
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
shardedJedis.disconnect();
}
/**
* 将哈希表 key 中的域 field 的值设为 value 。 如果 key 不存在,一个新的哈希表被创建并进行 HSET 操作。 如果域
* field 已经存在于哈希表中,旧值将被覆盖。
*
* @param key
* @param field
* @param value
* @return
*/
public Long hset(String key, String field, String value) {
Long result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = shardedJedis.hset(key, field, value);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 将哈希表 key 中的域 field 的值设为 value 。 如果 key 不存在,一个新的哈希表被创建并进行 HSET 操作。 如果域
* field 已经存在于哈希表中,旧值将被覆盖。
*
* @param key
* @param field
* @param value
* @return
*/
public Long hsetObject(String key, String field, Object object) {
Long result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = shardedJedis.hset(key.getBytes(), field.getBytes(),
SerializationUtils.serialize(object));
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 返回哈希表 key 中给定域 field 的值。
*
* @param key
* @param field
* @return
*/
public String hget(String key, String field) {
String result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = shardedJedis.hget(key, field);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 返回哈希表 key 中给定域 field 的值。
*
* @param key
* @param field
* @return
*/
public Object hgetObject(String key, String field) {
Object result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = SerializationUtils.deserialize(shardedJedis.hget(
key.getBytes(), field.getBytes()));
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。 若域 field 已经存在,该操作无效。 如果
* key 不存在,一个新哈希表被创建并执行 HSETNX 命令。
*
* @param key
* @param field
* @param value
* @return
*/
public Long hsetnx(String key, String field, String value) {
Long result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = shardedJedis.hsetnx(key, field, value);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 同时将多个 field-value (域-值)对设置到哈希表 key 中。 此命令会覆盖哈希表中已存在的域。
*
* @param key
* @param hash
* @return
*/
public String hmset(String key, Map
hash) {
String result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = shardedJedis.hmset(key, hash);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 同时将多个 field-value (域-值)对设置到哈希表 key 中。 此命令会覆盖哈希表中已存在的域。
*
* @param key
* @param hash
* @return
*/
public String hmsetObject(String key, Map hashs) {
String result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
Map byteHash = new HashMap();
for (Map.Entry setMap : hashs.entrySet()) {
String keyMap = setMap.getKey();
Object valMap = setMap.getValue();
byteHash.put(keyMap.getBytes(),
SerializationUtils.serialize(valMap));
}
result = shardedJedis.hmset(key.getBytes(), byteHash);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 同时将多个 field-value (域-值)对设置到哈希表 key 中。 此命令会覆盖哈希表中已存在的域。
*
* @param key
* @param hash
* @return
*/
public String hmsetMapObject(String key, Map> hashs) {
String result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
Map byteHash = new HashMap();
for (Map.Entry> setMap : hashs.entrySet()) {
String keyMap = setMap.getKey();
Object valMap = setMap.getValue();
byteHash.put(keyMap.getBytes(),
SerializationUtils.serialize(valMap));
}
result = shardedJedis.hmset(key.getBytes(), byteHash);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 返回哈希表 key 中,一个或多个给定域的值。 如果给定的域不存在于哈希表,那么返回一个 nil 值。
*
* @param key
* @param fields
* @return
*/
public List hmget(String key, String... fields) {
List result = null;
ShardedJedis shardedJedis = null;
boolean broken = false;
try {
shardedJedis = redisDataSource.getRedisClient();
result = shardedJedis.hmget(key, fields);
} catch (Exception e) {
logger.error(e.getMessage(), e);
broken = true;
}finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 返回哈希表 key 中,一个或多个给定域的值。 如果给定的域不存在于哈希表,那么返回一个 nil 值。
*
* @param key
* @param fields
* @return
*/
public List