Java RedisClient
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的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- JAVA实现的异步redisclient
再使用redis的过程中,发现使用缓存尽管好,可是有些地方还是比較难权衡,缓存对象大了,存储对象时的序列化工作非常繁重,消耗大量cpu:那么切分成非常小的部分吧,存取的次数变多了,redisclien ...
- Java NIO浅析
NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也是I/O多路复用的基础,已经被越来越多地应用到大型应用服务器,成为解决高并发与大量连接 ...
- java对redis的基本操作
一.server端安装 1.下载 https://github.com/MSOpenTech/redis 可看到当前可下载版本:redis2.6
- java对redis的基本操作(转)
本文转自:http://www.cnblogs.com/edisonfeng/p/3571870.html 2.主要类 1)功能类 package com.redis; import java.uti ...
- java操作redis之jedis篇
首先来简单介绍一下jedis,其实一句话就可以概括的,就是java操作redis的一种api.我们知道redis提供了基本上所有常用编程语言的clients,大家可以到http://redis.io/ ...
- Java连接redis的使用演示样例
Java连接redis的使用演示样例 Redis是开源的key-value存储工具,redis通经常使用来存储结构化的数据,由于redis的key能够包括String.hash.listset和sor ...
- 【原】实战-Java如何使用Redis
实战-Java如何使用Redis Redis的Client支持的语言非常丰富,如下: ActionScript Bash C C# C++ Clojure Common Lisp Crystal D ...
- Java中如何使用Redis做缓存
基本功能测试 1.程序基本结构 2.主要类 1)功能类 package com.redis; import java.util.ArrayList; import java.util.Iterator ...
随机推荐
- ASP.NET MVC案例教程(五)
ASP.NET MVC案例教程(四) 前言 通过前几篇文章,我们已经能比较自如的使用ASP.NET MVC来呈现页面和数据了.但是,有一个大问题没有解决:如何处理表单数据.例如,我们将要实现的公告发布 ...
- 线程同步——lock锁
线程同步即解决线程安全问题的第三种方式——使用lock锁 代码实现: 其中,ReentrantLock是lock接口的实现类,这边是使用多态创建,访问成员方法时,编译看左,运行看右: Reentran ...
- Class_fourth
动手动脑 1,多层异常捕捉一 2,多层异常捕捉二 3,EmbedFinally.java示例 最先截获的错误 最后输出finally 4, SystemExitAndFinally.java示例 如果 ...
- TLS握手、中断恢复与证书中心的原因
在双方都拿到随机数A.B.C后,将会使用这三个随机数生成一个对话密钥,然后使用该对话密钥进行对称加密通信,这种方式我们可以看到,安全性取决于随机数C的加密,前面的几个都是明文传的,这里就取决于服务器的 ...
- 【Bcftools】合并不同sample的vcf文件,通过bcftools
通过GATK calling出来的SNP如果使用UnifiedGenotype获得的SNP文件是分sample的,但是如果使用vcftools或者ANGSD则需要Vcf文件是multi-sample的 ...
- C语言实例:数组与字符串
数组: #include <stdio.h> #include <stdlib.h> #define ARRAY_SIZE(Array) (sizeof (Array) / s ...
- 1-趣味解读DNS工作原理——转载疯猫网络科技
因为只要我们输入百度.腾讯.淘宝的名字,无论它们的服务器在哪里,历经多少轮查询,我们都能找到并访问之.这就是计算机网络中著名的域名系统DNS(Domain Name System),它能实现把一个网站 ...
- iOS UIAlertController在iPhone与iPad上的区别
很简单的一段代码: // 首先声明一个UIAlertController对象 private var alertController: UIAlertController! // 初始化UIAlert ...
- ssm+redis整合(通过cache方式)
这几天的研究ssm redis 终于进入主题了,今天参考了网上一些文章搭建了一下ssm+redis整合,特别记录下来以便以后可以查询使用,有什么不足请大牛们提点 项目架构 1.pom.xml < ...
- .NET Core 如何上传文件及处理大文件上传
当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发ASP.NET Core MVC的Controller中的A ...