jedis

在RedisTemplate中使用scan代替keys指令

♀尐吖头ヾ 提交于 2019-11-27 07:18:44
keys * 这个命令千万别在生产环境乱用。特别是数据庞大的情况下。因为Keys会引发Redis锁,并且增加Redis的CPU占用。很多公司的运维都是禁止了这个命令的 当需要扫描key,匹配出自己需要的key时,可以使用 scan 命令 scan 操作的Helper实现 import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.ScanOptions; import org.springframework.data.redis.core.StringRedisTemplate; import

Why is data getting stored with weird keys in Redis when using Jedis with Spring Data?

最后都变了- 提交于 2019-11-27 06:33:45
I am using Spring Data Redis with Jedis. I am trying to store a hash with key vc:${list_id} . I was able to successfully insert to redis. However, when I inspect the keys using the redis-cli, I don't see the key vc:501381 . Instead I see \xac\xed\x00\x05t\x00\tvc:501381 . Why is this happening and how do I change this? arun Ok, googled around for a while and found help at http://java.dzone.com/articles/spring-data-redis . It happened because of Java serialization. The key serializer for redisTemplate needs to be configured to StringRedisSerializer i.e. like this: <bean id=

Springboot——redis入门

做~自己de王妃 提交于 2019-11-27 05:24:27
前言 互联网应用中,NoSQL已经广泛应用。Redis是一种内存数据库,Redis可以在1S内完成10万次的操作,针对查询比较大的应用,使用redis作为缓存,可以极大程度提高应用运行效率,为了总结这篇博客,之前已经花了时间进行了redis入门级别的学习,弄清了redis底层的相关操作命令,这个在后面进行总结。但是redis的内容不止有这些,还有redis事务,redis的事务机制可以有效保证在高并发的场景下数据的一致性,针对redis事务的操作,这篇博客后续再进行更新,目前这篇博客只能对spring和springboot中对redis的操作进行总结。 spring中针对redis的操作 redis是一种键值对数据库。但是不管如何,其依旧是一种数据库,spring针对数据访问的操作其实都大同小异,提供对应的ConnectionFactory接口,然后获取对应的connection对象,之后利用对应的connection对象进行数据访问的操作。 但是针对redis的数据访问又有些不同,这个也是因为redis本身的数据类型所致 大体上与其他的数据访问层没有二异,但是我们操作的时候有三种可以选择的连接对象,JedisConnection、JedisClusterConnection、DefaultStringRedisConnection三个,这个也是因为redis本身的特性和功能决定的

redis中如何存储java对象

空扰寡人 提交于 2019-11-27 03:22:57
根据redis的存储原理,Redis的key和value都支持二进制安全的字符串 1.利用序列化和反序列化的方式 存储java对象我们可以通过对象的序列化与反序列化完成存储于取出,这样就可以使用redis存储java对象了 a.利用jdk自带的序列化机制,但效率不高 步骤:创建一个序列化和反序列化的工具类 public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { //序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { } return null; } public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { //反序列化 bais =

Java开发包Jedis

断了今生、忘了曾经 提交于 2019-11-27 02:18:25
Jedis: http://www.oschina.net/p/jedis (Redis的官方首选Java开发包) <!--Redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.0.0</version> <type>jar</type> <scope>compile</scope> </dependency> 测试例子原帖:http://flychao88.iteye.com/blog/1527163 import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class JedisUtilTest { JedisPool pool; Jedis jedis;

Can Jedis get/set an Java POJO?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 01:21:50
问题 I'm using Jedis as the java client to connect to Redis Servers. Question 1: It seems there is no method to get/set Object < ? extends Serializable> ? All the values must be String or byte[]? Other clients like "JRedis" and Spymemcache(for memcached Server) could. Question 2: If I use ShardedJedis, it cannot set auth/password? While Jedis class can (using auth(String password)). 回答1: Regard Question 1 : Jedis won't handle POJOs. You should serialize to a string or byte[] and use jedis to do

Redis

扶醉桌前 提交于 2019-11-26 22:45:13
安装 (1)Mac HomeBrew $ brew install redis (2)按部就班 # 需要前置依赖 make、gcc $ sudo apt-get update $ sudo apt install make $ sudo apt-get install gcc # 下载、解压和编译 redis $ wget http://download.redis.io/releases/redis-5.0.3.tar.gz $ tar xzf redis-5.0.3.tar.gz $ cd redis-5.0.3 $ make 命令行使用 启动 Redis Server $./src/redis-server 进入 Redis 命令行 (1)本地连接 $redis-cli redis 127.0.0.1:6379> redis 127.0.0.1:6379> PING PONG (2)远程连接 $ redis-cli -h host -p port -a password 取键相关命令 # 查找所有 key > KEYS * # 查找符合给定正则的 key > KEYS pattern # 删除某个 key (适用于各种数据结构的 key) > DEL a_key # 检查某个 key 是否存在 > EXISTS a_key # 以秒为单位,返回给定 key 的剩余生存时间

java获取redis中各种数据类型key对应的value代码简单封装

折月煮酒 提交于 2019-11-26 22:08:41
目前在做自动化测试时,设计到需要获取存储在redis中的值,总结了操作代码如下: 需要jar包:jedis-2.7.3.jar、commons-pool2-2.4.1.jar code如下: package cn.migu.utils; import java.util.Iterator; import java.util.List; import cn.migu.base.GlobalSettings; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * <Description>redis相关操作类 * @author YanLu * */ public class RedisUtil { private JedisPool pool=null; private Jedis redis = null; Log4jUtil log = new Log4jUtil(this.getClass().getName()); //构造函数,创建对象时进行初始化 public RedisUtil() { if (pool == null) { /* // 池基本配置 //JedisPoolConfig

java中Redis5大基本类型的操作--jedis用法

大憨熊 提交于 2019-11-26 22:06:51
存储格式 大家都知道redis支持的存储类型(String/List/Hash/Set/SortedSet ),但是不一定在工作中都用到过,希望通过整理的这篇文章,让初学者都能知道在java中如何使用redis以及redis对这几种数据类型的操作。 基本用法 jedis就是集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。一般不直接使用jedis,而是在其上在封装一层,作为业务的使用。如果用spring的话,可以看看spring 封装的 redis Spring Data Redis Jedis工具类 public class JedisPoolUtil { private static JedisPool pool = null; static { //加载配置文件 InputStream in = JedisPoolUtil.class.getClassLoader().getResourceAsStream("redis.properties"); Properties pro = new Properties(); try { pro.load(in); } catch (IOException e) { e.printStackTrace(); System.out.println("加载文件失败"); } JedisPoolConfig

SpringBoot整合Redis Lettuce

烈酒焚心 提交于 2019-11-26 19:36:59
最近在准备集成的基础框架使用Spring Boot2搭建,其中Redis的支持不仅仅是丰富了它的API,更是替换掉底层Jedis的依赖,取而代之换成了Lettuce(生菜) jedis跟lettuce的区别 Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然都可以直接连接redis server。 Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接 Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。 导入依赖 在 pom.xml 中spring-boot-starter-data-redis的依赖,Spring Boot2.x 后底层不在是Jedis如果做版本升级的朋友需要注意下,第二个commons-pool2依赖不能少(连接池使用)。 <dependency> <groupId>org.springframework.boot</groupId>