package org.rx.util;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisShardInfo; import java.nio.charset.Charset;
import java.util.Map;
import java.util.Set; /**
* Created by wangxiaoming on 2016/3/29.
*
* @author http://blog.csdn.net/java2000_wl
*/
@Component
public class RedisClient {
private static RedissonClient redisson; //https://github.com/mrniko/redisson/wiki/8.-Distributed-locks-and-synchronizers
public synchronized static RedissonClient getRedisson() {
if (redisson == null) { Map<String, String> map = App.readSettings("app");
Config config = new Config();
config.useSingleServer().setAddress(String.format("%s:%s", map.get("redis.host"), map.get("redis.port")))
.setTimeout(App.convert(map.get("redis.timeout"), Integer.class));
redisson = Redisson.create(config);
}
return redisson;
} private static RedisTemplate<String, Object> Template;
@Autowired
private RedisTemplate<String, Object> template;
private String keyPrefix; public String getKeyPrefix() {
return keyPrefix;
} public void setKeyPrefix(String keyPrefix) {
if (template != null) {
throw new IllegalArgumentException("Autowired Instance");
}
this.keyPrefix = keyPrefix;
} private RedisTemplate<String, Object> getTemplate() {
if (template == null && Template == null) {
Map<String, String> map = App.readSettings("app");
JedisShardInfo config = new JedisShardInfo(map.get("redis.host"), Integer.parseInt(map.get("redis.port")));
JedisConnectionFactory fac = new JedisConnectionFactory(config);
fac.setTimeout(App.convert(map.get("redis.timeout"), Integer.class));
fac.setUsePool(true);
Template = new RedisTemplate<>();
Template.setConnectionFactory(fac);
Template.setKeySerializer(
new org.springframework.data.redis.serializer.StringRedisSerializer(Charset.forName("UTF8")));
Template.setValueSerializer(
new org.springframework.data.redis.serializer.JdkSerializationRedisSerializer());
Template.afterPropertiesSet();
}
return App.isNull(template, Template);
} private byte[] getKeyBytes(String key) {
try {
key = App.isNull(keyPrefix, "") + key;
return key.getBytes(App.UTF8);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} public void set(String key, Object value) {
this.set(key, value, 0L);
} public void set(final String key, final Object value, final long liveTime) {
getTemplate().execute(new RedisCallback() {
public Long doInRedis(RedisConnection client) throws DataAccessException {
byte[] theKey = getKeyBytes(key);
client.set(theKey, App.serialize(value));
if (liveTime > 0) {
client.expire(theKey, liveTime);
}
return 1L;
}
});
} public Object get(final String key) {
return getTemplate().execute(new RedisCallback() {
public Object doInRedis(RedisConnection client) throws DataAccessException {
byte[] theKey = getKeyBytes(key);
byte[] theVal = client.get(theKey);
if (theVal == null || theVal.length == 0) {
return null;
}
return App.deserialize(theVal);
}
});
} public long del(final String... keys) {
return (long) getTemplate().execute(new RedisCallback() {
public Long doInRedis(RedisConnection client) throws DataAccessException {
long result = 0;
for (String key : keys) {
result += client.del(getKeyBytes(key));
}
return result;
}
});
} public Set<String> keys(String pattern) {
return getTemplate().keys(pattern);
} public long dbSize() {
return (long) getTemplate().execute(new RedisCallback<Object>() {
public Long doInRedis(RedisConnection client) throws DataAccessException {
return client.dbSize();
}
});
} public boolean exists(final String key) {
return (boolean) getTemplate().execute(new RedisCallback() {
public Boolean doInRedis(RedisConnection client) throws DataAccessException {
return client.exists(getKeyBytes(key));
}
});
} public void flushDb() {
getTemplate().execute(new RedisCallback() {
public Object doInRedis(RedisConnection client) throws DataAccessException {
client.flushDb();
return null;
}
});
}
}

  

        <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.5.0</version>
</dependency>

Java RedisClient的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. JAVA实现的异步redisclient

    再使用redis的过程中,发现使用缓存尽管好,可是有些地方还是比較难权衡,缓存对象大了,存储对象时的序列化工作非常繁重,消耗大量cpu:那么切分成非常小的部分吧,存取的次数变多了,redisclien ...

  3. Java NIO浅析

    NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也是I/O多路复用的基础,已经被越来越多地应用到大型应用服务器,成为解决高并发与大量连接 ...

  4. java对redis的基本操作

    一.server端安装 1.下载 https://github.com/MSOpenTech/redis 可看到当前可下载版本:redis2.6

  5. java对redis的基本操作(转)

    本文转自:http://www.cnblogs.com/edisonfeng/p/3571870.html 2.主要类 1)功能类 package com.redis; import java.uti ...

  6. java操作redis之jedis篇

    首先来简单介绍一下jedis,其实一句话就可以概括的,就是java操作redis的一种api.我们知道redis提供了基本上所有常用编程语言的clients,大家可以到http://redis.io/ ...

  7. Java连接redis的使用演示样例

    Java连接redis的使用演示样例 Redis是开源的key-value存储工具,redis通经常使用来存储结构化的数据,由于redis的key能够包括String.hash.listset和sor ...

  8. 【原】实战-Java如何使用Redis

    实战-Java如何使用Redis Redis的Client支持的语言非常丰富,如下: ActionScript Bash C C# C++ Clojure Common Lisp Crystal D ...

  9. Java中如何使用Redis做缓存

    基本功能测试 1.程序基本结构 2.主要类 1)功能类 package com.redis; import java.util.ArrayList; import java.util.Iterator ...

随机推荐

  1. jmeter对自身性能的优化

    测试环境 apache-jmeter-2.13   1.   问题描述 单台机器的下JMeter启动较大线程数时可能会出现运行报错的情况,或者在运行一段时间后,JMeter每秒生成的请求数会逐步下降, ...

  2. kylin对接hive实现实时查询

     前提: 安装kylin之前,需要安装hadoop2.0.hbase.hive,并且对版本有要求,可以参照官网链接 http://kylin.apache.org/cn/docs/install/in ...

  3. javascript时间处理

    1.将一般格式时间转换为时间戳: var systime = "2018年04月28日 16:01:09"; systime = systime.replace('年', &quo ...

  4. (转) SpringMVC学习笔记-

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--conf ...

  5. CSS 使用技巧

    CSS 使用技巧 1.CSS代码重用,解决同一类样式下相同冲突点 <style> .c { 共有 } .c1 { 独有 } .c2 { 独有 } </style> <di ...

  6. spring mvc 的上传图片是怎么实现的?

    spring mvc 的上传图片是怎么实现的? 导入jar包,commons-io.jar 及 commons-fileupload.jar 在springmvc的配置文件中配置Mutipart解析器 ...

  7. Codeforces 979E Kuro and Topological Parity - 动态规划 - 组合数学

    题目传送门 传送点 题目大意 给定$n$个标号依次为$1, 2, \cdots, n$的点,其中一些点被染成一些颜色,剩下的点没有染色.你需要添加一些有向边并将剩下的点染色,满足有向边从编号小的一端指 ...

  8. JAVASCRIPT 分层概念

    1)底层(框架提供): 封装DOM和Event相关操作,提供跨浏览器兼容的接口,扩展原生javascript语言本身不提供的但又特实用的接口,例如namespace; 2)抽象类层(框架提供 统一自定 ...

  9. HDU3033 I love sneakers!———分组背包

    这题的动态转移方程真是妙啊,完美的解决了每一种衣服必须买一件的情况. if(a[x][i-c[x][j].x]!=-1) a[x][i]=max(a[x][i],a[x][i-c[x][j].x]+c ...

  10. vi删除当前行的字符

    x 删除当前光标下的字符dw 删除光标之后的单词剩余部分.d$ 删除光标之后的该行剩余部分.dd 删除当前行