赞
踩
黑马课程Redis笔记,供个人学习用,若有侵权,联系删除
Redis是一个基于内存的key-value结构数据库,即非关系型数据库,与MySQL二维表格数据库不同。并不是取代MySQL,而是对MySQL的补充
链接:https://pan.baidu.com/s/1mIxr8IEWj-vdLSl7EJnovw?pwd=w8ah
提取码:w8ah
①启动Redis服务端:redis-server.exe redis.windows.conf
②启动客户端,连接Redis服务:redis-cli.exe (默认连接本地Redis服务,即6379
测试Redis服务启动是否正确:keys *
③也可以连接指定IP和端口号的Redis服务
④更改redis服务的密码
⑤重新启动redis服务,再启动客户端,输入密码,连接服务
链接:https://pan.baidu.com/s/1gwBh5AcMagAKHEk5W5vEQw?pwd=umxc
提取码:umxc
Redis存储的是key-value结构的数据,其中key是字符串类型,value有5种常用的数据类型:
Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象,常用命令:
Redis列表是简单的字符串列表,按照插入顺序排序,常用命令:
Redis set是string类型的无序集合。集合成员是唯一的,集合中不能出现重复的数据,常用命令:
Redis有序集合是string类型元素的集合,且不允许有重复成员。每个元素都会关联一个double类型的分数。常用命令:
Redis的Java客户端很多,常用的几种:
spring Data Redis是spring的一部分,对Redis 底层开发包进行了高度封装。在spring项目中,可以使用spring Data Redis来简化操作。
操作步骤:
①导入Spring Data Redis的maven坐标
-
- <!--Spring data redis -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
-
-
②配置Redis数据源
- spring:
- # redis配置
- redis:
- host: localhost
- port: 6379
- password: 123456
- database: 0
③编写配置类,创建RedisTemplate对象
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
- /**
- * Redis配置类
- */
- @Configuration
- @Slf4j
- public class RedisConfiguration {
-
- @Bean
- public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
- RedisTemplate redisTemplate = new RedisTemplate();
- //设置Redis的连接工厂
- redisTemplate.setConnectionFactory(redisConnectionFactory);
- //设置Redis key的序列化器
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- return redisTemplate;
- }
- }

④通过RedisTemplate对象操作Redis
1)操作字符串类型的数据
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.*;
-
- import java.util.concurrent.TimeUnit;
-
- @SpringBootTest
- public class SpringDataRedisTest {
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- @Test
- public void testRedistTemplate() {
- System.out.println(redisTemplate);
- ValueOperations valueOperations = redisTemplate.opsForValue();//字符串
- ListOperations listOperations = redisTemplate.opsForList();//列表
- HashOperations hashOperations = redisTemplate.opsForHash();//哈希
- SetOperations setOperations = redisTemplate.opsForSet();//集合
- ZSetOperations zSetOperations = redisTemplate.opsForZSet();//有序集合
- }
-
- /**
- * 操作字符串类型的数据
- */
- @Test
- public void testString(){
- // set get setex setnx
- redisTemplate.opsForValue().set("city", "潜江");
- String city = (String) redisTemplate.opsForValue().get("city");
- System.out.println(city);
- redisTemplate.opsForValue().set("code", "1234", 3, TimeUnit.MINUTES);
- redisTemplate.opsForValue().setIfAbsent("lock", "1");
- redisTemplate.opsForValue().setIfAbsent("lock", "2");
- }
- }

2)操作哈希类型的数据
- /**
- * 操作哈希类型的数据
- */
- @Test
- public void testHash(){
- //hset hget hdel hkeys hvals
- HashOperations hashOperations = redisTemplate.opsForHash();
- //hset
- hashOperations.put("100", "name", "张三");
- hashOperations.put("100", "age", "20");
- //hget
- String name = (String) hashOperations.get("100", "name");
- System.out.println(name);
- //hkeys
- Set keys = hashOperations.keys("100");
- System.out.println(keys);
- //hvals
- List values = hashOperations.values("100");
- System.out.println(values);
- //hdel
- hashOperations.delete("100", "age");
- }

3)操作列表类型的数据
- /**
- * 操作列表类型的数据
- */
- @Test
- public void testList() {
- //lpush lrange rpop llen
- ListOperations listOperations = redisTemplate.opsForList();
- //lpush
- listOperations.leftPushAll("mylist", "a", "b", "c");
- listOperations.leftPush("mylist", "d");
- //lrange
- List mylist = listOperations.range("mylist", 0, -1);
- System.out.println(mylist);
- //rpop
- listOperations.rightPop("mylist");
- //llen
- Long size = listOperations.size("mylist");
- System.out.println(size);
- }

4)操作集合类型的数据
- /**
- * 操作集合类型的数据
- */
- @Test
- public void testSet() {
- //sadd smembers scard sinter sunion srem
- SetOperations setOperations = redisTemplate.opsForSet();
- //sadd
- setOperations.add("set1", "a", "b", "c", "d");
- setOperations.add("set2", "a", "b", "x", "y");
- //smembers
- Set set1 = setOperations.members("set1");
- System.out.println(set1);
- //scard
- Long size = setOperations.size("set1");
- System.out.println(size);
- //sinter
- Set intersect = setOperations.intersect("set1", "set2");
- System.out.println(intersect);
- //sunion
- Set union = setOperations.union("set1", "set2");
- System.out.println(union);
- //srem
- setOperations.remove("set1", "a", "b");
- }

5)操作有序集合类型的数据
- /**
- * 操作有序集合类型的数据
- */
- @Test
- public void testZset() {
- //zadd zrange zincrby zrem
- ZSetOperations zSetOperations = redisTemplate.opsForZSet();
- //zadd
- zSetOperations.add("zset1", "a", 10);
- zSetOperations.add("zset1", "b", 12);
- zSetOperations.add("zset1", "c", 9);
- //zrange
- Set zset1 = zSetOperations.range("zset1", 0, -1);
- System.out.println(zset1);
- //zincrby
- zSetOperations.incrementScore("zset1", "c", 10);
- //zrem
- zSetOperations.remove("zset1", "a","b");
- }

6)通用命令的操作
- /**
- * 通用命令操作
- */
- @Test
- public void testCommon() {
- //keys exists type del
- //keys
- Set keys = redisTemplate.keys("*");
- System.out.println(keys);
- //exists
- Boolean name = redisTemplate.hasKey("name");
- Boolean set1 = redisTemplate.hasKey("set1");
- //type
- for (Object key: keys) {
- DataType type = redisTemplate.type(key);
- System.out.println(type.name());
- }
- //del
- redisTemplate.delete("mylist");
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。