redis学习

news/2024/10/23 5:47:11/

Redis

1.安装

 vi /etc/sysconfig/network-scripts/ifcfg-ens33  #修改网络

安装gcc依赖

yum install -y gcc tclcd redis-6.2.11
make && make installcp redis.conf.bck redis.conf
#修改redis.conf
bind 0.0.0.0
daemonize yes
requirepass 123456cd /usr/local/src/redis-6.2.11/src
redis-server /usr/local/src/redis-6.2.11/redis.confredis-cli -h 192.168.0.138 -p 6379 -a 123456

下载客户端

https://github.com/lework/RedisDesktopManager-Windows/releases

String类型常见命令

keys *
del age
EXISTS name
expire age 30
ttl age
incr age
incrby age 2
set score 10.1
INCRBYFLOAT score 0.5
setnx name lisi
setex  name 10 jack

2.redis使用

2.1常用的操作redis的客户端工具

jedis
Jedis 是 Redis 官方推荐的 Java 连接开发工具,jedis非线程安全。

但是可以通过JedisPool连接池去管理实例,在多线程情况下让每个线程有自己独立的jedis实例,可变为线程安全。

Lettuce
Lettuce 是基于 Netty框架(NIO)的事件驱动的通信,支持同步和异步调用的,可扩展的 redis client,多个线程可以共享一个 RedisConnection,线程安全。

SpringBoot2.0之后默认都是使用的Lettuce这个客户端连接Redis服务器。Lettuce 是一种可扩展的、线程安全的 Redis 高级客户端。

Redisson
Redisson 是一个在 Redis 的功能基础上实现的 Java 驻内存数据网格客户端。实现了分布式和可扩展的 Java 数据结构,提供很多分布式相关操作服务,例如分布式锁,分布式集合,可通过 Redis 支持延迟队列。

小结
Jedis 和 Lettuce 两者相比,Jedis 的性能比较差,其他方面并没有太明显的区别,所以如果你不需要使用 Redis 的高级功能的话,优先推荐使用 Lettuce。

相比于 Jedis、Lettuce 等基于 redis 命令封装的客户端,Redisson 提供的功能更加高端和抽象,逼格简单使用

2.2Jedis测试

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.0</version>
</dependency>
private Jedis jedis;@BeforeEach
void setUp() {jedis = new Jedis("192.168.0.138", 6379);jedis.auth("123456");jedis.select(0);
}@Test
void testString() {String result = jedis.set("name", "胡歌");System.out.println("result = " + result);String name = jedis.get("name");System.out.println(name);
}@Test
void testHash(){jedis.hset("user:1","name","jack");jedis.hset("user:1","age","21");//获取Map<String, String> map = jedis.hgetAll("user:1");System.out.println(map);
}@AfterEach
void tearDown() {if (jedis != null) jedis.close();
}

Jedis连接池

Jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,因此我们推荐大家使用Jedis连接池代替Jedis的连接方式。

通过JedisPool连接池去管理实例,在多线程情况下让每个线程有自己独立的jedis实例,可变为线程安全。

public class JedisConctionFactory {private static final JedisPool jedisPool;static {JedisPoolConfig poolConfig = new JedisPoolConfig();//设置最大连接数poolConfig.setMaxTotal(8);//设置最大空闲连接数poolConfig.setMaxIdle(6);// 设置最小空闲连接数 (就是隔段时间后,一直没有连接,会销毁剩于至1)poolConfig.setMinIdle(1);poolConfig.setMaxWaitMillis(1000);jedisPool = new JedisPool(poolConfig,"192.168.0.138",6379,1000,"123456");}public static Jedis getJedis(){return jedisPool.getResource();}
}
@SpringBootTest
public class RedisApplicationTests {private Jedis jedis;@BeforeEachvoid setUp() {
//        jedis = new Jedis("192.168.0.138", 6379);jedis = JedisConctionFactory.getJedis();jedis.auth("123456");jedis.select(0);}@Testvoid testString() {String result = jedis.set("name", "胡歌");System.out.println("result = " + result);String name = jedis.get("name");System.out.println(name);}@Testvoid testHash(){jedis.hset("user:1","name","jack");jedis.hset("user:1","age","21");//获取Map<String, String> map = jedis.hgetAll("user:1");System.out.println(map);}@AfterEachvoid tearDown() {if (jedis != null) jedis.close();}
}

2.3SpringDataRedis

  • 提供了对不同Redis客户端的整合(Lettuce和Jedis)
  • RedisTemplate统一操作
  • 支持发布订阅
  • 支持哨兵和集群
  • 支持基于Lettuce的响应式编程
  • 支持基于JDK、JSON、字符串、Spring对象的数据序列化和反序列化(如原始的jedis客户端只是支持String)
  • 支持基于Redis的JDKCollection实现
<!--        redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
<!--        连接池依赖--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
spring:redis:host: 192.168.0.138port: 6379password: 123456jedis:pool:max-active: 8max-idle: 8min-idle: 0max-wait: 1000ms

String类型

不序列化 默认采用JDK序列化

@Resource
private RedisTemplate redisTemplate;@Test
public void test1(){redisTemplate.opsForValue().set("name","虎哥");Object name = redisTemplate.opsForValue().get("name");System.out.println("name:" + name);
}
\xAC\xED\x00\x05t\x00\x06\xE8\x99\x8E\xE5\x93\xA5

序列化

@Configuration
public class redisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();template.setKeySerializer(RedisSerializer.string());template.setHashKeySerializer(RedisSerializer.string());template.setValueSerializer(jsonSerializer);template.setHashValueSerializer(jsonSerializer);return template;}
}
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Test
public void test2(){redisTemplate.opsForValue().set("user:100",new User("虎哥",21));User user = (User) redisTemplate.opsForValue().get("user:100");System.out.println("user:" + user);
}

查看redis数据库

{"@class": "com.cc.redis.pojo.User","name": "虎哥","age": 21
}

问题:多了 "@class": "com.cc.redis.pojo.User", 占用空间,优化方式:使用手动序列化

手动序列化不需要上面的序列化配置了

@Resource
private StringRedisTemplate stringRedisTemplate;private static final ObjectMapper mapper = new ObjectMapper();@Test
public void test2() throws Exception{User user = new User("虎哥", 21);String josn = mapper.writeValueAsString(user);stringRedisTemplate.opsForValue().set("user:200",josn);String jsonUser = stringRedisTemplate.opsForValue().get("user:200");User user1 = mapper.readValue(jsonUser, User.class);System.out.println("user:" + user);
}@Test
public void test1(){stringRedisTemplate.opsForValue().set("name","虎哥");Object name = stringRedisTemplate.opsForValue().get("name");System.out.println("name:" + name);
}

查看数据库

{"name": "虎哥","age": 21
}

Hash类型

@Test
public void test3(){stringRedisTemplate.opsForHash().put("user:400","name","虎哥");stringRedisTemplate.opsForHash().put("user:400","age","21");Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:400");System.out.println("entries:" + entries);
}

http://www.ppmy.cn/news/46312.html

相关文章

Attention注意力机制

加粗样式通俗理解&#xff1a;你会注意什么&#xff1f; 对于一个模型而言&#xff08;CNN&#xff0c;LSTM&#xff09;&#xff0c;模型本身很难决定什么重要什么不重要&#xff0c;因此注意力机制诞生了。 注意力机制&#xff1a;我们会把焦点聚焦在比较重要的事务上 怎么…

一本通 3.4.6 拓扑排序

1352&#xff1a;【例4-13】奖金 【题目描述】 由于无敌的凡凡在2005年世界英俊帅气男总决选中胜出&#xff0c;Yali Company总经理Mr.Z心情好&#xff0c;决定给每位员工发奖金。公司决定以每个人本年在公司的贡献为标准来计算他们得到奖金的多少。 于是Mr.Z下令召开m方会谈…

Markdown 使用 Emoji 表情 MD格式小表情大全

文章目录 Markdown 使用 Emoji 表情 && MD格式小表情大全# 复制和粘贴表情符号# 使用表情符号简码MD格式小表情大全结语Markdown 使用 Emoji 表情 && MD格式小表情大全 有两种方法可以将表情符号添加到Markdown文件中:将表情符号复制并粘贴到Markdown格式的文…

1.4 Docker Swarm-详细介绍

Docker Swarm 是 Docker 官方推出的容器编排工具&#xff0c;用于管理 Docker 容器集群。Docker Swarm 的主要功能包括容器的部署、扩容、缩容、更新等。本文将详细介绍 Docker Swarm 的相关概念、架构、部署和使用方法。 一、Docker Swarm 概述 Docker Swarm 是 Docker 官方…

10、数据库学习规划:MySQL - 学习规划系列文章

MySQL数据库是笔者认识的几个流行的数据库之一。类似于Linux重装系统&#xff0c;其也是开源的&#xff0c;最主要是有很多的社区支持&#xff0c;众多的开发者对其能够进行使用&#xff0c;所以其功能也挺强大&#xff0c;便于使用。通过对MySQL数据库的学习&#xff0c;笔者认…

《计算机算法设计与分析》课后练习07

Author:龙箬 Computer Application Technology Change the World with Data and Artificial Intelligence ! CSDNweixin_43975035 我行及我道 //算法&#xff1a;用A(m)划分集合A(m:p-1) procedure PARTITION(m,p)integer m,p,i; global A(m:p-1)v A(m); i m //A(m)是划分元素…

FVM链的Themis Pro,5日ido超百万美元

交易一直是 DeFi 乃至web3领域最经久不衰的话题&#xff0c;也因此催生了众多优秀的去中心化协议&#xff0c;如 Uniswap 和 Curve。这些协议逐渐成为了整个系统的基石。 在永续合约方面&#xff0c;DYDX 的出现将 WEB2 时代的订单簿带回了web3。其链下交易的设计&#xff0c;仿…

VMware vSphere 8.0 Update 1 正式版发布 - 企业级工作负载平台

ESXi 8.0 U1 & vCenter Server 8.0 U1 请访问原文链接&#xff1a;https://sysin.org/blog/vmware-vsphere-8-u1/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org 2023-04-18&#xff0c;VMware vSphere 8.0 Update 1 正式…