|
看到一篇关于redis和spring集成的文章,实际测试后,可以。转载以备用。谢谢 亲昵YY!
一.准备要使用的的jar包,我使用了以下jar包,关于json的jar包是方便存储和转换redis用到的值;

二.编写加载redis是要用到的实例类
- package com.redis;
-
- import redis.clients.jedis.ShardedJedis;
-
-
-
-
-
-
-
-
-
- public interface RedisDataSource {
-
- public abstract ShardedJedis getRedisClient();
-
- public void returnResource(ShardedJedis shardedJedis);
-
- public void returnResource(ShardedJedis shardedJedis,boolean broken);
-
- }
- package com.redis;
-
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import redis.clients.jedis.ShardedJedis;
- import redis.clients.jedis.ShardedJedisPool;
-
-
-
-
-
-
-
-
-
- public class RedisDataSourceImpl implements RedisDataSource{
-
- private static final org.slf4j.Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class);
-
- @Autowired
- private ShardedJedisPool shardedJedisPool;
-
- public ShardedJedisPool getShardedJedisPool() {
- return shardedJedisPool;
- }
-
- public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
- this.shardedJedisPool = shardedJedisPool;
- }
-
-
-
-
- public ShardedJedis getRedisClient() {
- try {
- ShardedJedis shardedJedis=shardedJedisPool.getResource();
- return shardedJedis;
- } catch (Exception e) {
- log.error("getRedisClient ,error",e);
- e.printStackTrace();
- }
- return null;
- }
-
-
-
-
- @SuppressWarnings("deprecation")
- public void returnResource(ShardedJedis shardedJedis) {
- shardedJedisPool.returnResource(shardedJedis);
-
- }
-
-
-
-
- @SuppressWarnings("deprecation")
- public void returnResource(ShardedJedis shardedJedis, boolean broken) {
- if(broken){
- shardedJedisPool.returnBrokenResource(shardedJedis);
- }else{
- shardedJedisPool.returnResource(shardedJedis);
- }
-
- }
-
- }
- package com.redis;
-
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisShardInfo;
- import redis.clients.jedis.ShardedJedis;
-
-
-
-
-
-
-
-
-
- public class RedisClientTemplate {
-
- private static final org.slf4j.Logger log=LoggerFactory.getLogger(RedisClientTemplate.class);
-
- @Autowired
- private RedisDataSource redisDataSource;
-
- public RedisDataSource getRedisDataSource() {
- return redisDataSource;
- }
-
- public void setRedisDataSource(RedisDataSource redisDataSource) {
- this.redisDataSource = redisDataSource;
- }
-
- public void disconnect(){
- ShardedJedis shardedJedis=redisDataSource.getRedisClient();
- shardedJedis.disconnect();
- }
-
-
-
-
-
-
- public String set(String key, String value){
- String result=null;
- ShardedJedis shardedJedis=redisDataSource.getRedisClient();
- if(shardedJedis==null){
- return result;
- }
- boolean broken=false;
- try {
- result=shardedJedis.set(key, value);
- } catch (Exception e) {
- broken=true;
- e.printStackTrace();
- }finally{
- redisDataSource.returnResource(shardedJedis, broken);
- }
-
- return result;
- }
-
-
-
-
-
-
- public String get(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.get(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Boolean exists(String key) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.exists(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public String type(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.type(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long expire(String key, int seconds) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.expire(key, seconds);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long expireAt(String key, long unixTime) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.expireAt(key, unixTime);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long ttl(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.ttl(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long setnx(String key, String value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.setnx(key, value);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public String setex(String key, int seconds, String value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.setex(key, seconds, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long decrBy(String key, long integer) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.decrBy(key, integer);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Long decr(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.decr(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long incrBy(String key, long integer) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.incrBy(key, integer);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Long incr(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.incr(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public Long append(String key, String value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.append(key, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public String substr(String key, int start, int end) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.substr(key, start, end);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public Long hset(String key, String field, String value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hset(key, field, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public String hget(String key, String field) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hget(key, field);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public String hmset(String key, Map<String, String> hash) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hmset(key, hash);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public List<String> hmget(String key, String... fields) {
- List<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hmget(key, fields);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public Long hincrBy(String key, String field, long value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hincrBy(key, field, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Boolean hexists(String key, String field) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hexists(key, field);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Long del(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.del(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long hdel(String key, String field) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hdel(key, field);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Long hlen(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hlen(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Set<String> hkeys(String key) {
- Set<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hkeys(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public List<String> hvals(String key) {
- List<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hvals(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Map<String, String> hgetAll(String key) {
- Map<String, String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.hgetAll(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
-
- public Long rpush(String key, String string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.rpush(key, string);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long lpush(String key, String string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lpush(key, string);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Long llen(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.llen(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public List<String> lrange(String key, long start, long end) {
- List<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lrange(key, start, end);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public String ltrim(String key, long start, long end) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.ltrim(key, start, end);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public String lindex(String key, long index) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lindex(key, index);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public String lset(String key, long index, String value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lset(key, index, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public String lpop(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.lpop(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public String rpop(String key) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.rpop(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
- public Long sadd(String key, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.sadd(key, member);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Set<String> smembers(String key) {
- Set<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.smembers(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long scard(String key) {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- Long result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.scard(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public Long zadd(String key, double score, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zadd(key, score, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public Set<String> zrange(String key, int start, int end) {
- Set<String> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zrange(key, start, end);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long zrem(String key, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.zrem(key, member);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public Double zincrby(String key, double score, String member) {
- Double result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zincrby(key, score, member);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long zrank(String key, String member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zrank(key, member);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Long zcard(String key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zcard(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Double zscore(String key, String member) {
- Double result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zscore(key, member);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
-
- public Long zcount(String key, double min, double max) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zcount(key, min, max);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
- public String set(byte[] key, byte[] value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.set(key, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public byte[] get(byte[] key) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.get(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
- public Boolean exists(byte[] key) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.exists(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long expire(byte[] key, int seconds) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.expire(key, seconds);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long expireAt(byte[] key, long unixTime) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.expireAt(key, unixTime);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long ttl(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.ttl(key);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long append(byte[] key, byte[] value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.append(key, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
-
-
- public Long hset(byte[] key, byte[] field, byte[] value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.hset(key, field, value);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public byte[] hget(byte[] key, byte[] field) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.hget(key, field);
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public String hmset(byte[] key, Map<byte[], byte[]> hash) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.hmset(key, hash);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public List<byte[]> hmget(byte[] key, byte[]... fields) {
- List<byte[]> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.hmget(key, fields);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
- public Boolean hexists(byte[] key, byte[] field) {
- Boolean result = false;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.hexists(key, field);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long hdel(byte[] key, byte[] field) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.hdel(key, field);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long rpush(byte[] key, byte[] string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.rpush(key, string);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long lpush(byte[] key, byte[] string) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.lpush(key, string);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long llen(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.llen(key);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public List<byte[]> lrange(byte[] key, int start, int end) {
- List<byte[]> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.lrange(key, start, end);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
- public String lset(byte[] key, int index, byte[] value) {
- String result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.lset(key, index, value);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
- public Long lrem(byte[] key, int count, byte[] value) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.lrem(key, count, value);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public byte[] lpop(byte[] key) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.lpop(key);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public byte[] rpop(byte[] key) {
- byte[] result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.rpop(key);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
-
-
-
-
- public Long sadd(byte[] key, byte[] member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.sadd(key, member);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Set<byte[]> smembers(byte[] key) {
- Set<byte[]> result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.smembers(key);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long scard(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.scard(key);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Long zadd(byte[] key, double score, byte[] member) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zadd(key, score, member);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
-
- public Long zcard(byte[] key) {
- Long result = null;
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
-
- result = shardedJedis.zcard(key);
-
- } catch (Exception e) {
-
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public JedisShardInfo getShardInfo(String key) {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- JedisShardInfo result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.getShardInfo(key);
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Collection<JedisShardInfo> getAllShardInfo() {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- Collection<JedisShardInfo> result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.getAllShardInfo();
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- public Collection<Jedis> getAllShards() {
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- Collection<Jedis> result = null;
- if (shardedJedis == null) {
- return result;
- }
- boolean broken = false;
- try {
- result = shardedJedis.getAllShards();
-
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- broken = true;
- } finally {
- redisDataSource.returnResource(shardedJedis, broken);
- }
- return result;
- }
-
- }
三.配置spring xml文件
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
-
-
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxTotal" value="2048" />
- <property name="maxIdle" value="200" />
- <property name="numTestsPerEvictionRun" value="1024"/>
- <property name="timeBetweenEvictionRunsMillis" value="30000" />
- <property name="minEvictableIdleTimeMillis" value="-1" />
- <property name="softMinEvictableIdleTimeMillis" value="10000" />
- <property name="maxWaitMillis" value="1500"/>
- <property name="testOnBorrow" value="true" />
- <property name="testWhileIdle" value="true"/>
- <property name="testOnReturn" value="false"/>
- <property name="jmxEnabled" value="true"/>
- <property name="blockWhenExhausted" value="false"/>
- </bean>
-
- <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
- <constructor-arg index="0" ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg name="host" value="127.0.0.1" />
- <constructor-arg name="port" value="6379" />
- <constructor-arg name="timeout" value="10000" />
- </bean>
- </list>
- </constructor-arg>
- </bean>
- <bean id="redisDataSource" class="redis.dao.RedisDataSourceImpl">
- <property name="shardedJedisPool" ref="shardedJedisPool"></property>
- </bean>
- <bean id="redisClientTemplate" class="redis.dao.RedisClientTemplate">
- <property name="redisDataSource" ref="redisDataSource"></property>
- </bean>
- </beans>
四.代码中的使用,原则就是在执行service时,先通过key到redis里检测是否存在,如果存在则在redis里去数据,如果没有则到数据库去数据,然后存到redis里
- public class StudentinfoServiceImpl implements StudentinfoService{
-
- private StudentinfoDao studentinfoDao;
-
- private RedisClientTemplate redisClientTemplate;
-
- private static final ObjectMapper mapper=new ObjectMapper();
-
- public List<Studentinfo> getStudentinfoByStu(Studentinfo stu) {
- String key="getStudentinfoByStu:usernumber:"+stu.getUsernumber()+":name:"+stu.getName()+":idnumber:"+stu.getIdnumber();
- try {
- boolean flag=redisClientTemplate.exists(key);
- if(flag){
- String stuJsonStr=redisClientTemplate.get(key);
- List<Studentinfo> list=mapper.readValue(stuJsonStr, new TypeReference<List<Studentinfo>>() {});
- return list;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- String hql="from Studentinfo where 1=1";
- if(stu!=null){
- if(stu.getUsernumber()!=null){
- hql+=" and usernumber like '%"+stu.getUsernumber()+"%' ";
- }
- }
- List<Studentinfo> list = studentinfoDao.getStudentinfoByStu(hql);
- try {
- String stuJsonStr=mapper.writeValueAsString(list);
- redisClientTemplate.setex(key, 60, stuJsonStr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return list;
- }
-
-
- public StudentinfoDao getStudentinfoDao() {
- return studentinfoDao;
-
- }
-
- public void setStudentinfoDao(StudentinfoDao studentinfoDao) {
- this.studentinfoDao = studentinfoDao;
- }
-
- public RedisClientTemplate getRedisClientTemplate() {
- return redisClientTemplate;
- }
-
- public void setRedisClientTemplate(RedisClientTemplate redisClientTemplate) {
- this.redisClientTemplate = redisClientTemplate;
- }
-
- }
来源: http://blog.csdn.net/u012498149/article/details/53170719
|