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. 【Spark-core学习之六】 Spark资源调度和任务调度

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  2. Mysql报错:Packet for query is too large (1121604 > 1048576).You can change this value on the server by setting the max_allowed_packet variable

    看错误信息,发现1048576个字节,正好是1*1024*1024byte,也就是1Mb. 这正是mysql默认的max_allowed_packet值. 使用sql语句: show VARIABLE ...

  3. Linux进程相关命令使用场景

    Linux进程相关命令使用场景 在Linux系统上工作时,我们常常会碰到很多和进程相关的查询场景,今天在这里进行详细的讲解,进程相关的对象包括以下几个: 端口:Port 进程号:PId 执行文件所在路 ...

  4. 微信公众号开发流程,jssdk的使用以及签名算法的实现

    一 开发流程 1 基本配置-登录自己的公众号 A:新型微信认证,认证过的企业号才可以进行自定义菜单中的连接跳转: B:开发基本配置里面进行开发者iD查询,密码查询和重置和ip白名单配置: C:公众号设 ...

  5. K8S学习笔记之ETCD启动失败注意事项

    最近搭建K8S集群遇到ETCD的报错,报错信息如下,一定要关闭防火墙.iptables和SELINUX,三个都要关闭!! Mar 26 20:39:24 k8s-m1 etcd[6437]: heal ...

  6. Bugku-CTF之flag在index里

      Day15 flag在index里 http://123.206.87.240:8005/post/      

  7. Linux统计文件中单词出现的次数

    grep -E "\b[[:alpha:]]+\b"  /etc/fstab  -o | sort | uniq -c 或 awk '{for(i=1;i<NF;i++){c ...

  8. linux 新建用户、用户组 以及为新用户分配权限的基本操作

    分享下Linux系统中创建用户.设置密码.修改用户.删除用户的命令: 创建用户:useradd testuser  创建用户testuser设置密码:passwd testuser  给已创建的用户t ...

  9. Linux ssh下实现免密码登录

    1.Linux 生成密钥 ssh-keygen -t rsa 进入“.ssh”会生成以下几个文件 id_rsa : 生成的私钥文件 id_rsa.pub : 生成的公钥文件 know_hosts : ...

  10. webpack引入eslint详解