原文:http://blog.csdn.net/centre10/article/details/50761815
http://my.oschina.net/u/866380/blog/521658 (切片方式的带密码、数据库方式的访问)
http://www.cnblogs.com/zr520/p/5062025.html (spring 使用redis集群配置)
要用redis ,则必须添加相关的包
jedis-2.7.2.jar
commons-pool2-2.0.jar
1、spring 和 redis 的非切片方式的配置
<!-- Jedis 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 控制一个pool(池)可分配多少个jedis实例,通过pool.getResource()来获取;如果赋值为-1,则表示不限制 -->
<property name="maxTotal" value="${jedis.pool.maxTotal}" />
<!-- 控制一个pool(池)最多有多少个状态为idle(空闲)的jedis实例 -->
<property name="maxIdle" value="${jedis.pool.maxIdle}" />
<!-- 表示当borrow(借用)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->
<property name="maxWaitMillis" value="${jedis.pool.maxWaitMillis}" />
<!-- 在borrow(借用)一个jedis实例时,是否提前进行validate(验证)操作;如果为true,则得到的jedis实例均是可用的 -->
<property name="testOnBorrow" value="${jedis.pool.testOnBorrow}" />
<!-- 在return(返回)给pool(池)时,是否提前进行validate(验证)操作 -->
<property name="testOnReturn" value="${jedis.pool.testOnReturn}" />
</bean>
<!-- JedisPool(非切片链接池)配置 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg ref="jedisPoolConfig" />
<constructor-arg value="${redis.hostName}" />
<constructor-arg value="${redis.port}" type="int" />
<constructor-arg value="${redis.timeout}" type="int" />
<constructor-arg value="${redis.password}" />
<constructor-arg value="${redis.database}" type="int"/>
</bean>
redis.properites配置如下:
redis.hostName=172.168.48.197
redis.port=6379
redis.timeout=3000
redis.password=chbigdata
redis.database=1
jedis.pool.maxTotal=300
jedis.pool.maxIdle=300
jedis.pool.maxWaitMillis=100000
jedis.pool.testOnBorrow=true
jedis.pool.testOnReturn=true
java 类中,jedis 2.7.2版本中的释放资源jedis与2.6之前的版本有区别,示例:
/**
*
* <p>说明:存入对象到缓存</p>
* <p>时间:2016-06-27 14:21:41</p>
* @param key
* 键
* @param object
* 对象
* @return boolean
*/
public boolean putObject(String key, Object object) {
boolean flag = false;
if (StringUtils.isBlank(key)) {
return flag;
}
if (object == null) {
return flag;
}
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String result = jedis.set(key, JSONObject.toJSONString(object, SerializerFeature.WriteClassName));
if ("ok".equalsIgnoreCase(result)) {
flag = true;
}
} catch (Exception e) {
logger.error("Redis putObject error" + e.getMessage(), e);
}finally {
if (jedis != null) {
try {
jedis.close();
}catch (Exception e) {
logger.error("Jedis putObject method close error" + e.getMessage(), e);
}
}
}
return flag;
}
2、哨兵方式sentinels的集群配置方式请看文章:http://www.cnblogs.com/zr520/p/5062025.html
3、下边来说说spring 和 redis 集群的配置(高可用配置,配置2个实例,则spring默认是启用第1个实例,当第1个实例不可用时,再启用第2个实例;)
将以下XML配置引入spring
<!-- jedis 多个服务器配置-->
配置写法1:与下边配置写法2任意选一种即可,我选的此种方式
<bean id="jedisShardInfo1" class="redis.clients.jedis.JedisShardInfo">
<!-- URL方式要求 uri在redis.properies 中配置为:
redis.uri1=redis://:chbigdata@172.17.123.134:6379/9
即redis://:[密码]@[服务器地址]:[端口]/[db index]-->
<constructor-arg name="host" value="${redis.uri1}" />
</bean>
<bean id="jedisShardInfo2" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.uri2}" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedisShardInfo1" />
<ref bean="jedisShardInfo2"/>
</list>
</constructor-arg>
</bean>
配置写法2:其实就是上边的一个变种
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1">
<list>
<bean name="slaver" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.uri1}" />
</bean>
<bean name="master" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.uri2}" />
</bean>
</list>
</constructor-arg>
</bean>
<!-- jedisPoolConfig 必须要-->
<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="jmxNamePrefix" value="chbigdata"/>
<property name="blockWhenExhausted" value="false"/>
</bean>
properties配置文件:
#redis配置
redis.slaver.host=127.0.0.1
redis.slaver.port=6379
redis.master.host=127.0.0.2
redis.master.port=6379
将shardedJedisPool注入相关的工具类中即可使用
import java.util.ArraygeiList;ge
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* <p>标题:</p>
* <p>描述:</p>
* <p>Copyright:Copyright(c) 2015 yjph83</p>
* <p>日期:2016年6月27日</p>
* @author yjph83
*/
public class JedisUtil {
private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);
@Resource
private ShardedJedisPool shardedJedisPool;
/**
* 设置一个key的过期时间(单位:秒)
*
* @param key
* key值
* @param seconds
* 多少秒后过期
* @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间
*/
public long expire(String key, int seconds) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.expire(key, seconds);
} catch (Exception ex) {
logger.error("shardedJedis expire error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis expire method close error" + e.getMessage(), e);
}
}
return 0;
}
/**
* 设置一个key在某个时间点过期
*
* @param key
* key值
* @param unixTimestamp
* unix时间戳,从1970-01-01 00:00:00开始到现在的秒数
* @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间
*/
public long expireAt(String key, int unixTimestamp) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.expireAt(key, unixTimestamp);
} catch (Exception ex) {
logger.error("shardedJedis expireAt error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis expireAt method close error" + e.getMessage(), e);
}
}
return 0;
}
/**
* 截断一个List
*
* @param key
* 列表key
* @param start
* 开始位置 从0开始
* @param end
* 结束位置
* @return 状态码
*/
public String trimList(String key, long start, long end) {
if (StringUtils.isBlank(key)) {
return "-";
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.ltrim(key, start, end);
} catch (Exception ex) {
logger.error("shardedJedis trimList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis trimList method close error" + e.getMessage(), e);
}
}
return "-";
}
/**
* 检查Set长度
*
* @param key
* @return
*/
public long countSet(String key) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.scard(key);
} catch (Exception ex) {
logger.error("shardedJedis countSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis countSet method close error " + e.getMessage(), e);
}
}
return 0;
}
/**
* 添加到Set中(同时设置过期时间)
*
* @param key
* key值
* @param seconds
* 过期时间 单位s
* @param value
* @return
*/
public boolean addSet(String key, int seconds, String... value) {
boolean result = addSet(key, value);
if (result) {
long i = expire(key, seconds);
return i == 1;
}
return false;
}
/**
* 添加到Set中
*
* @param key
* @param value
* @return
*/
public boolean addSet(String key, String... value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (value.length == 0 || value == null){
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.sadd(key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis addSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* @param key
* @param value
* @return 判断值是否包含在set中
*/
public boolean containsInSet(String key, String value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (StringUtils.isBlank(value)){
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.sismember(key, value);
} catch (Exception ex) {
logger.error("shardedJedis setList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis containsInSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获取Set
*
* @param key
* @return
*/
public Set<String> getSet(String key) {
if (StringUtils.isBlank(key)) {
return null;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.smembers(key);
} catch (Exception ex) {
logger.error("shardedJedis getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 从set中删除value
*
* @param key
* @return
*/
public boolean removeSetValue(String key, String... value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (value.length == 0 || value == null){
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.srem(key, value);
return true;
} catch (Exception ex) {
logger.error("getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis removeSetValue method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 从list中删除value 默认count 1
*
* @param key
* @param values
* 值list
* @return
*/
public int removeListValue(String key, List<String> values) {
return removeListValue(key, 1, values);
}
/**
* 从list中删除value
*
* @param key
* @param count
* @param values
* 值list
* @return
*/
public int removeListValue(String key, long count, List<String> values) {
if (StringUtils.isBlank(key)) {
return 0;
}
if (values.size() == 0 || values == null){
return 0;
}
int result = 0;
if (values != null && values.size() > 0) {
for (String value : values) {
if (removeListValue(key, count, value)) {
result++;
}
}
}
return result;
}
/**
* 从list中删除value
*
* @param key
* @param count
* 要删除个数
* @param value
* @return
*/
public boolean removeListValue(String key, long count, String value) {
if (StringUtils.isBlank(key)) {
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.lrem(key, count, value);
return true;
} catch (Exception ex) {
logger.error("getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis removeListValue method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 截取List
*
* @param key
* @param start
* 起始位置
* @param end
* 结束位置
* @return
*/
public List<String> rangeList(String key, long start, long end) {
if (StringUtils.isBlank(key)) {
return null;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.lrange(key, start, end);
} catch (Exception ex) {
logger.error("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis rangeList method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 检查List长度
*
* @param key
* @return
*/
public long countList(String key) {
if (StringUtils.isBlank(key)) {
return 0;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.llen(key);
} catch (Exception ex) {
logger.error("countList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis countList method close error." + e.getMessage(), e);
}
}
return 0;
}
/**
* 添加到List中(同时设置过期时间)
*
* @param key
* key值
* @param seconds
* 过期时间 单位s
* @param ... value
* @return
*/
public boolean addList(String key, int seconds, String... value) {
boolean result = addList(key, value);
if (result) {
long i = expire(key, seconds);
return i == 1;
}
return false;
}
/**
* 添加到List
*
* @param key
* @param ... value
* @return
*/
public boolean addList(String key, String... value) {
if (key == null || value == null) {
return false;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.lpush(key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis addList method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 添加到List(只新增)
*
* @param key
* @param List values
* @return
*/
public boolean addList(String key, List<String> list) {
if (StringUtils.isBlank(key) || list == null || list.size() == 0) {
return false;
}
for (String value : list) {
addList(key, value);
}
return true;
}
/**
* 获取List
*
* @param key
* @return
*/
public List<String> getList(String key) {
if (StringUtils.isBlank(key)) {
return null;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.lrange(key, 0, -1);
} catch (Exception ex) {
logger.error("shardedJedis getList error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getList method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 设置HashSet对象
*
* @param domain
* 域名
* @param key
* 键值
* @param value
* Json String or String value
* @return
*/
public boolean setHSet(String domain, String key, String value) {
if (StringUtils.isBlank(key)) {
return false;
}
if (value == null)
return false;
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.hset(domain, key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis setHSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获得HashSet对象
*
* @param domain
* 域名
* @param key
* 键值
* @return Json String or String value
*/
public String getHSet(String domain, String key) {
if (StringUtils.isBlank(key))
return null;
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.hget(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis getHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getHSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 删除HashSet对象
*
* @param domain
* 域名
* @param key
* 键值
* @return 删除的记录数
*/
public long delHSet(String domain, String key) {
long count = 0;
if (StringUtils.isBlank(key)) {
return count;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
count = shardedJedis.hdel(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis delHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis delHSet method close error." + e.getMessage(), e);
}
}
return count;
}
/**
* 删除HashSet对象
*
* @param domain
* 域名
* @param ... key
* 键值
* @return 删除的记录数
*/
public long delHSet(String domain, String... key) {
long count = 0;
if (key.length == 0 || key == null) {
return count;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
count = shardedJedis.hdel(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis delHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis delHSet method close error." + e.getMessage(), e);
}
}
return count;
}
/**
* 判断key是否存在
*
* @param domain
* 域名
* @param key
* 键值
* @return
*/
public boolean existsHSet(String domain, String key) {
boolean isExist = false;
if (StringUtils.isBlank(key)) {
return isExist;
}
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
isExist = shardedJedis.hexists(domain, key);
} catch (Exception ex) {
logger.error("shardedJedis existsHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis existsHSet method close error." + e.getMessage(), e);
}
}
return isExist;
}
/**
* 全局扫描hset
*
* @param match
* field匹配模式
* @return
*/
public List<Map.Entry<String, String>> scanHSet(String domain, String match) {
ShardedJedis shardedJedis = null;
try {
int cursor = 0;
shardedJedis = shardedJedisPool.getResource();
ScanParams scanParams = new ScanParams();
scanParams.match(match);
Jedis jedis = shardedJedis.getShard(domain);
ScanResult<Map.Entry<String, String>> scanResult;
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
do {
scanResult = jedis.hscan(domain, String.valueOf(cursor),
scanParams);
list.addAll(scanResult.getResult());
cursor = Integer.parseInt(scanResult.getStringCursor());
} while (cursor > 0);
return list;
} catch (Exception ex) {
logger.error("shardedJedis scanHSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis scanHSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 返回 domain 指定的哈希集中所有字段的value值
*
* @param domain
* @return
*/
public List<String> hvals(String domain) {
ShardedJedis shardedJedis = null;
List<String> retList = null;
try {
shardedJedis = shardedJedisPool.getResource();
retList = shardedJedis.hvals(domain);
} catch (Exception ex) {
logger.error("shardedJedis hvals error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis hvals method close error." + e.getMessage(), e);
}
}
return retList;
}
/**
* 返回 domain 指定的哈希集中所有字段的key值
*
* @param domain
* @return
*/
public Set<String> hkeys(String domain) {
ShardedJedis shardedJedis = null;
Set<String> retList = null;
try {
shardedJedis = shardedJedisPool.getResource();
retList = shardedJedis.hkeys(domain);
} catch (Exception ex) {
logger.error("shardedJedis hkeys error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis hkeys method close error." + e.getMessage(), e);
}
}
return retList;
}
/**
* 返回 domain 指定的哈希key值总数
*
* @param domain
* @return
*/
public long lenHset(String domain) {
ShardedJedis shardedJedis = null;
long retList = 0;
try {
shardedJedis = shardedJedisPool.getResource();
retList = shardedJedis.hlen(domain);
} catch (Exception ex) {
logger.error("shardedJedis lenHset error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis lenHset method close error." + e.getMessage(), e);
}
}
return retList;
}
/**
* 设置指定位置的String类型的值排序集合
*
* @param key
* @param score
* @param value
* @return
*/
public boolean setSortedSet(String key, long score, String value) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.zadd(key, score, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis setSortedSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis setSortedSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 根据String类型的key,起始、结束记录位置获得排序集合
*
* @param key
* @param startScore
* @param endScore
* @param orderByDesc
* @return
*/
public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
if (orderByDesc) {
return shardedJedis.zrevrangeByScore(key, endScore, startScore);
} else {
return shardedJedis.zrangeByScore(key, startScore, endScore);
}
} catch (Exception ex) {
logger.error("shardedJedis getSoredSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getSoredSet method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 根据String类型的key,起始、结束记录位置计算排序长度
*
* @param key
* @param startScore
* @param endScore
* @return
*/
public long countSoredSet(String key, long startScore, long endScore) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
Long count = shardedJedis.zcount(key, startScore, endScore);
return count == null ? 0L : count;
} catch (Exception ex) {
logger.error("shardedJedis countSoredSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis countSoredSet method close error." + e.getMessage(), e);
}
}
return 0L;
}
/**
* 根据String类型的key和value删除排序集合
*
* @param key
* @param value
* @return
*/
public boolean delSortedSet(String key, String value) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
long count = shardedJedis.zrem(key, value);
return count > 0;
} catch (Exception ex) {
logger.error("shardedJedis delSortedSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis delSortedSet method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获得排序集合
*
* @param key
* @param startRange
* @param endRange
* @param orderByDesc
* @return
*/
public Set<String> getSoredSetByRange(String key, int startRange, int endRange, boolean orderByDesc) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
if (orderByDesc) {
return shardedJedis.zrevrange(key, startRange, endRange);
} else {
return shardedJedis.zrange(key, startRange, endRange);
}
} catch (Exception ex) {
logger.error("shardedJedis getSoredSetByRange error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getSoredSetByRange method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 获得排序打分
*
* @param key
* @return
*/
public Double getScore(String key, String member) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.zscore(key, member);
} catch (Exception ex) {
logger.error("shardedJedis getSoredSet error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis getScore method close error." + e.getMessage(), e);
}
}
return null;
}
/**
* 存入String的值,并设置过期时间
* @param key
* @param value
* @param second 秒
* @return
*/
public boolean set(String key, String value, int second) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.setex(key, second, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis set error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis set method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 存入String 类型的值
* @param key
* @param value
* @return
*/
public boolean set(String key, String value) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.set(key, value);
return true;
} catch (Exception ex) {
logger.error("shardedJedis set error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis set method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 获取值,如果值为空,则可以指定返回值
* @param key
* @param defaultValue
* @return
*/
public String get(String key, String defaultValue) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.get(key) == null ? defaultValue : shardedJedis.get(key);
} catch (Exception ex) {
logger.error("shardedJedis get error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis get method close error." + e.getMessage(), e);
}
}
return defaultValue;
}
/**
* 根据key来删除值
* @param key
* @return
*/
public boolean del(String key) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.del(key);
return true;
} catch (Exception ex) {
logger.error("shardedJedis del error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis del method close error." + e.getMessage(), e);
}
}
return false;
}
/**
* 名称为key的string加1操作
* @param key
* @return
*/
public long incr(String key) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.incr(key);
} catch (Exception ex) {
logger.error("shardedJedis incr error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis incr method close error." + e.getMessage(), e);
}
}
return 0;
}
/**
* 名称为key的string减1操作
* @param key
* @return
*/
public long decr(String key) {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.decr(key);
} catch (Exception ex) {
logger.error("shardedJedis decr error.", ex);
} finally {
try{
if (shardedJedis != null ){
shardedJedis.close();
}
}catch (Exception e) {
logger.error("shardedJedis decr method close error." + e.getMessage(), e);
}
}
return 0;
}
}
相关推荐
redis3.0 widows集群 spring整合jedis redis搭建window集群代码和文档rubygems-2.5.1和rubyinstaller-2.2.3-x64软件下载
(4)集群:Redis 3.0支持集群,可以将多个Redis实例拼接成一个集群,并实现负载均衡,提高Redis的可用性。 3、总结: Redis 3.0是一款高性能的开源内存数据库,它拥有非常快速的读写性能,几乎是传统数据库的1000倍...
### Redis 3.0 集群指南 #### 一、Redis 3.0 的安装与配置 **安装步骤:** 在安装 Redis 3.0 之前,首先确保已安装了必要的依赖包,如 cpp、binutils、glibc 等。通过 `yum` 命令安装这些依赖: ```bash yum -y ...
安装完成后,Redis 集群将启动,用户可以通过 jedis 等客户端来访问集群。 知识点总结: * Redis 是一个开源、基于内存的数据结构存储系统。 * Redis 集群环境安装需要安装 gcc、ruby、rubygem 和 gem-redis 等...
Redis3.0集群代理系统, 并发接收客户端请求,计算Key的哈希槽值,转发到对应的缓存服务器,并将缓存服务器的返回值回传给客户端, 这样客户端只要访问集群代理系统,实现一次性定位访问,效率与单台缓存服务器...
### Redis 3.0 集群部署详细指南 #### 一、概述 Redis 3.0 是一个重要的里程碑版本,因为它引入了官方支持的集群功能。通过集群部署,可以实现数据分片、高可用性以及横向扩展能力,极大地提高了Redis在生产环境中...
Redis 3.0版本是其发展历程中的一个重要里程碑,它带来了许多改进和新特性,旨在提升性能、稳定性和功能多样性。 在Redis 3.0中,主要关注以下几个方面的更新: 1. **多线程I/O**: 以往的Redis版本是单线程模型,...
Redis 3.0 集群与应用是关于如何配置、管理和使用Redis分布式数据库系统的一个教程,旨在提供简单明了的指导。Redis是一款高性能的键值存储服务,常用于数据库缓存、消息队列等场景。在3.0版本中,引入了集群功能,...
而Jedis是Java语言的Redis客户端,提供了丰富的API来与Redis服务器进行交互。 Redis 3.0引入了多项增强和改进,包括: 1. **Cluster Support**: Redis 3.0首次实现了集群功能,允许用户将数据分布在多个节点上,...
虽然Redis 3.0不支持集群功能,但在更高版本中,Redis引入了集群方案,允许在多台服务器之间分散数据,提高可用性和容量。若需要扩展,可以考虑升级到更高版本或使用哨兵(Sentinel)系统实现高可用性。 综上所述,...
本资源“redis3.0-window免安装”是专门为Windows操作系统设计的Redis 3.0版本,旨在简化在Windows平台上的部署和使用流程。 Redis 3.0是一个重要的版本,它引入了多项新特性和改进,包括但不限于: 1. **多线程...
为此,Redis 3.0引入了集群功能,通过数据分片来实现水平扩展,从而支持更大的数据集与更高的并发访问。 #### 二、环境准备 本章节主要介绍如何搭建一个基本的Redis 3.0集群环境,包括必要的软件安装以及集群的配置...
linux下redis3.0 安装与优化
6. 测试Redis服务:为了验证Redis是否成功运行,可以使用Redis客户端工具,如`redis-cli.exe`。在同一命令提示符窗口中输入: ``` redis-cli ``` 接着,你可以尝试进行简单的操作,如`SET key value`和`GET key`...
Redis是世界上最受欢迎的开源键值存储系统之一,尤其在...总之,Redis 3.0在Windows上的部署和使用提供了在非Linux环境下体验Redis高效数据处理能力的机会,但需要注意与Unix环境的区别,并做好相应的系统配置和管理。
首先,集成Spring与Redis的关键在于`spring-data-redis`模块,它为Spring应用提供了操作Redis的高级抽象。你需要包含这个模块的jar文件,以确保能够使用Spring的RedisTemplate和StringRedisTemplate等工具进行数据...
通过这个“redis3.0 demo”,我们可以学习如何配置和使用Redis集群,如何编写和执行LUA脚本,以及如何利用其他新特性来优化应用性能。同时,它也将演示如何处理复制、连接管理和安全性等核心问题。通过实践,你可以...
这个版本是Redis 3.0.504的64位版本,适合在Windows操作系统上运行。它是一个绿色免安装版本,意味着你无需经过传统安装过程,只需将压缩包解压到任意目录,然后执行`startup.bat`脚本就可以启动Redis服务器。 1. *...