1、applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 对象池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}"/> <!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" /> <!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /> <!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> <!-- 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 -->
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
</bean> <!-- 分布式链接池 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton" destroy-method="destroy">
<constructor-arg index="" ref="jedisPoolConfig"/>
<constructor-arg index="">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.host1}" />
<constructor-arg name="port" value="${redis.port1}" />
<constructor-arg name="timeout" value="${redis.timeout}" />
</bean>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.host2}" />
<constructor-arg name="port" value="${redis.port2}" />
<constructor-arg name="timeout" value="${redis.timeout}" />
</bean>
</list>
</constructor-arg>
</bean> <bean id="redisService" class="org.saiku.redis.impl.RedisServiceImpl">
<property name="shardedJedisPool" ref="shardedJedisPool"></property>
</bean> <bean id="redisClient" class="org.saiku.redis.RedisClient">
<property name="redisService" ref="redisService"></property>
</bean> </beans>

2、RedisServiceImpl:获取和放回SharedJedis对象

接口

package org.saiku.redis;

import redis.clients.jedis.ShardedJedis;

public interface RedisService {

    public ShardedJedis getRedisClient();

    public void returnResource(ShardedJedis shardedJedis);

    public void returnResource(ShardedJedis shardedJedis, boolean broken);

}

实现类

package org.saiku.redis.impl;

import org.saiku.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool; @SuppressWarnings("deprecation")
public class RedisServiceImpl implements RedisService { private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);
private ShardedJedisPool shardedJedisPool; public ShardedJedisPool getShardedJedisPool() {
return shardedJedisPool;
} public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
} @Override
public ShardedJedis getRedisClient() {
try {
ShardedJedis shardJedis = shardedJedisPool.getResource();
return shardJedis;
} catch (Exception e) {
log.error("getRedisClent error", e);
}
return null;
} @Override
public void returnResource(ShardedJedis shardedJedis) {
shardedJedisPool.returnResource(shardedJedis);
} @Override
public void returnResource(ShardedJedis shardedJedis, boolean broken) {
if (broken) {
shardedJedisPool.returnBrokenResource(shardedJedis);
} else {
shardedJedisPool.returnResource(shardedJedis);
}
} }

3、RedisClient:执行对redis服务器数据的增删改查的类

package org.saiku.redis;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.saiku.redis.impl.RedisServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple; public class RedisClient { private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class); private RedisServiceImpl redisService; public RedisServiceImpl getRedisService() {
return redisService;
} public void setRedisService(RedisServiceImpl redisService) {
this.redisService = redisService;
} /**
* 释放连接
*/
public void disconnect() {
ShardedJedis shardedJedis = redisService.getRedisClient();
shardedJedis.disconnect();
} /**
* 设置单个值
*/
public String set(String key, String value) {
String result = null; ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 判断是否存在
*
* @param key
* @return
*/
public Boolean exists(String key) {
Boolean result = false;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.exists(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 获取单个值
*/
public String get(String key) {
String result = null; ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 判断是何种类型
*
* @param key
* @return
*/
public String type(String key) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.type(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 在某段时间后失效
*/
public Long expire(String key, int seconds) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.expire(key, seconds); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 在某个时间点失效
*/
public Long expireAt(String key, long time) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.expireAt(key, time); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 获取生存周期
*
* @param key
* @return
*/
public Long ttl(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.ttl(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public boolean setbit(String key, long offset, boolean value) { ShardedJedis shardedJedis = redisService.getRedisClient();
boolean result = false;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.setbit(key, offset, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public boolean getbit(String key, long offset) {
ShardedJedis shardedJedis = redisService.getRedisClient();
boolean result = false;
if (shardedJedis == null) {
return result;
}
boolean flag = false; try {
result = shardedJedis.getbit(key, offset);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public long setrange(String key, long offset, String value) {
ShardedJedis shardedJedis = redisService.getRedisClient();
long result = 0;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.setrange(key, offset, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String getrange(String key, long startOffset, long endOffset) {
ShardedJedis shardedJedis = redisService.getRedisClient();
String result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getrange(key, startOffset, endOffset); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String getSet(String key, String value) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getSet(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long setnx(String key, String value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.setnx(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String setex(String key, int seconds, String value) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.setex(key, seconds, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long decrBy(String key, long integer) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.decrBy(key, integer); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long decr(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.decr(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long incrBy(String key, long integer) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.incrBy(key, integer); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long incr(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.incr(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long append(String key, String value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.append(key, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String substr(String key, int start, int end) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.substr(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long hset(String key, String field, String value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hset(key, field, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String hget(String key, String field) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hget(key, field); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long hsetnx(String key, String field, String value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hsetnx(key, field, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String hmset(String key, Map<String, String> hash) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hmset(key, hash); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public List<String> hmget(String key, String... fields) {
List<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hmget(key, fields); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long hincrBy(String key, String field, long value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hincrBy(key, field, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Boolean hexists(String key, String field) {
Boolean result = false;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hexists(key, field); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long del(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.del(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long hdel(String key, String field) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hdel(key, field); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long hlen(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hlen(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> hkeys(String key) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hkeys(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public List<String> hvals(String key) {
List<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hvals(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Map<String, String> hgetAll(String key) {
Map<String, String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.hgetAll(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 在redis list尾部增加一个String
* */
public Long rpush(String key, String string) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.rpush(key, string); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 在redis list头部增加一个String
* */
public Long lpush(String key, String string) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.lpush(key, string); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long llen(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.llen(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public List<String> lrange(String key, long start, long end) {
List<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.lrange(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String ltrim(String key, long start, long end) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.ltrim(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String lIndex(String key, long index) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.lindex(key, index); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String lset(String key, long index, String value) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.lset(key, index, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long lrem(String key, long count, String value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.lrem(key, count, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 从redis list头部取出一个key
* */
public String lpop(String key) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.lpop(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} /**
* 从redis list尾部取出一个key
* */
public String rpop(String key) {
String result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.rpop(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long sadd(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.sadd(key, member); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> smembers(String key) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.smembers(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long srem(String key, String member) {
ShardedJedis shardedJedis = redisService.getRedisClient(); Long result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.srem(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String spop(String key) {
ShardedJedis shardedJedis = redisService.getRedisClient();
String result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.spop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long scard(String key) {
ShardedJedis shardedJedis = redisService.getRedisClient();
Long result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.scard(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Boolean sismember(String key, String member) {
ShardedJedis shardedJedis = redisService.getRedisClient();
Boolean result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.sismember(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String srandmember(String key) {
ShardedJedis shardedJedis = redisService.getRedisClient();
String result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.srandmember(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zadd(String key, double score, String member) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.zadd(key, score, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> zrange(String key, int start, int end) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.zrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zrem(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.zrem(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Double zincrby(String key, double score, String member) {
Double result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zincrby(key, score, member); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zrank(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrank(key, member); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zrevrank(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrevrank(key, member); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> zrevrange(String key, int start, int end) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrevrange(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<Tuple> zrangeWithScores(String key, int start, int end) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrangeWithScores(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<Tuple> zrevrangeWithScores(String key, int start, int end) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrevrangeWithScores(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zcard(String key) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zcard(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Double zscore(String key, String member) {
Double result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zscore(key, member); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public List<String> sort(String key) {
List<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.sort(key); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public List<String> sort(String key, SortingParams sortingParameters) {
List<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.sort(key, sortingParameters); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zcount(String key, double min, double max) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zcount(key, min, max); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> zrangeByScore(String key, double min, double max) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrangeByScore(key, min, max); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> zrevrangeByScore(String key, double max, double min) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrevrangeByScore(key, max, min); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> zrangeByScore(String key, double min, double max,
int offset, int count) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrangeByScore(key, min, max, offset, count); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<String> zrevrangeByScore(String key, double max, double min,
int offset, int count) {
Set<String> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis
.zrevrangeByScore(key, max, min, offset, count); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrangeByScoreWithScores(key, min, max); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<Tuple> zrevrangeByScoreWithScores(String key, double max,
double min) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrevrangeByScoreWithScores(key, max, min); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<Tuple> zrangeByScoreWithScores(String key, double min,
double max, int offset, int count) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrangeByScoreWithScores(key, min, max,
offset, count); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Set<Tuple> zrevrangeByScoreWithScores(String key, double max,
double min, int offset, int count) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zrevrangeByScoreWithScores(key, max, min,
offset, count); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zremrangeByRank(String key, int start, int end) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zremrangeByRank(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long zremrangeByScore(String key, double start, double end) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.zremrangeByScore(key, start, end); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Long linsert(String key, LIST_POSITION where, String pivot,
String value) {
Long result = null;
ShardedJedis shardedJedis = redisService.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try { result = shardedJedis.linsert(key, where, pivot, value); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} @SuppressWarnings("deprecation")
public List<Object> pipelined(ShardedJedisPipeline shardedJedisPipeline) {
ShardedJedis shardedJedis = redisService.getRedisClient();
List<Object> result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.pipelined(shardedJedisPipeline);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Jedis getShard(String key) {
ShardedJedis shardedJedis = redisService.getRedisClient();
Jedis result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getShard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public JedisShardInfo getShardInfo(String key) {
ShardedJedis shardedJedis = redisService.getRedisClient();
JedisShardInfo result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getShardInfo(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public String getKeyTag(String key) {
ShardedJedis shardedJedis = redisService.getRedisClient();
String result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getKeyTag(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Collection<JedisShardInfo> getAllShardInfo() {
ShardedJedis shardedJedis = redisService.getRedisClient();
Collection<JedisShardInfo> result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getAllShardInfo(); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} public Collection<Jedis> getAllShards() {
ShardedJedis shardedJedis = redisService.getRedisClient();
Collection<Jedis> result = null;
if (shardedJedis == null) {
return result;
}
boolean flag = false;
try {
result = shardedJedis.getAllShards(); } catch (Exception e) {
log.error(e.getMessage(), e);
flag = true;
} finally {
redisService.returnResource(shardedJedis, flag);
}
return result;
} }

4、使用

在需要使用redisClient的地方注入

@Autowired
private RedisClient redisClient;

调用redisClient方法执行增删改查

//不存在就新增
if( !redisClient.exists(auth.getName())){
redisClient.set(redisKey,userInfo.toString());
} //存在才可以删除
if( redisClient.exists(auth.getName())){
redisClient.del(redisKey);
} //获取
String userInfo = redisClient.get(username);

效果:通过查询redis可以看到数据成功的插入到redis了

127.0.0.1:6379> KEYS *
1) "user1"
127.0.0.1:6379> get user1
"user1;pwd2;b1f86e0a-8761-4467-b420-48d9783d3806;tl21uazmvnd4v3ee4bunesms;ROLE_USER"

[redis] 分布式 Redis 的 CRUD 实现的更多相关文章

  1. Redis分布式缓存剖析及大厂面试精髓v6.2.6

    概述 官方说明 Redis官网 https://redis.io/ 最新版本6.2.6 Redis中文官网 http://www.redis.cn/ 不过中文官网的同步更新维护相对要滞后不少时间,但对 ...

  2. 从Redis分布式缓存实战入手到底层原理分析、面面俱到覆盖大厂面试考点

    概述 官方说明 Redis官网 https://redis.io/ 最新版本6.2.6 Redis中文官网 http://www.redis.cn/ 不过中文官网的同步更新维护相对要滞后不少时间,但对 ...

  3. Redis分布式集群几点说道

    原文地址:http://www.cnblogs.com/verrion/p/redis_structure_type_selection.html  Redis分布式集群几点说道 Redis数据量日益 ...

  4. Codis——分布式Redis服务的解决方案

    Codis——分布式Redis服务的解决方案 之前介绍过的 Twemproxy 是一种Redis代理,但它不支持集群的动态伸缩,而codis则支持动态的增减Redis节点:另外,官方的redis 3. ...

  5. 基于redis分布式缓存实现(新浪微博案例)

    第一:Redis 是什么? Redis是基于内存.可持久化的日志型.Key-Value数据库 高性能存储系统,并提供多种语言的API. 第二:出现背景 数据结构(Data Structure)需求越来 ...

  6. Ping CAP CTO、Codis作者谈redis分布式解决方案和分布式KV存储

    此文根据[QCON高可用架构群]分享内容,由群内[编辑组]志愿整理,转发请注明出处. 苏东旭,Ping CAP CTO,Codis作者 开源项目Codis的co-author黄东旭,之前在豌豆荚从事i ...

  7. 一致性Hash算法在Redis分布式中的使用

    由于redis是单点,但是项目中不可避免的会使用多台Redis缓存服务器,那么怎么把缓存的Key均匀的映射到多台Redis服务器上,且随着缓存服务器的增加或减少时做到最小化的减少缓存Key的命中率呢? ...

  8. 基于redis分布式缓存实现

    Redis的复制功能是完全建立在之前我们讨论过的基 于内存快照的持久化策略基础上的,也就是说无论你的持久化策略选择的是什么,只要用到了Redis的复制功能,就一定会有内存快照发生,那么首先要注意你 的 ...

  9. elk+redis分布式分析nginx日志

    一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...

随机推荐

  1. SqlSever基础 union all 联合查询,简单的组合 两个查询结果拼在一起

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. Django 中的用户认证

    Django 自带一个用户认证系统,这个系统处理用户帐户.组.权限和基于 cookie 的 会话.本文说明这个系统是如何工作的. 概览 认证系统由以下部分组成: 用户 权限:控制用户进否可以执行某项任 ...

  3. Java EE 在网页输出九九乘法表

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. ZOJ 3819 Average Score(平均分)

    Description 题目描述 Bob is a freshman in Marjar University. He is clever and diligent. However, he is n ...

  5. [资源] Resources on Self-Driving Car

    Talk NVIDIA at CES 2016 - Self Driving Cars and Deep Learning GPUs Projects Autoware Open-source sof ...

  6. Spark.ML之PipeLine学习笔记

    地址: http://spark.apache.org/docs/2.0.0/ml-pipeline.html   Spark PipeLine 是基于DataFrames的高层的API,可以方便用户 ...

  7. yii2-datepicker/datetimepicker插件使用

    datepicker: https://github.com/2amigos/yii2-date-picker-widget 通过composer安装: composer require 2amigo ...

  8. iOS - NetRequest 网络数据请求

    1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ...

  9. SAP接口编程 之 JCo3.0系列(05) : Exception Handling

    JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...

  10. js模块化编程总结

    大家都知道,js中的变量(variable)有其作用范围,比如:函数里用var定义的变量在函数外是看不到的,而定义在函数外面的变量(不能有没有var修饰)均是全局变量,在js程序的任何位置都可以访问. ...