Redis入门
Redis是一个基于内存的key-value结构数据库,读写性能较高
Redis数据类型
Redis存储的是key-value结构的数据,其中key是字符串类型,value有5种数据类型:
1.字符串 string
2.哈希 hash
3.列表 list
4.集合 set
5.有序集合 sorted set
字符串string操作常用命令
设置值和取值(set&get)
设置存活时间(setex)
设置存活10s
setnx 当key不存在时才设置值
哈希hash操作命令
列表list常用命令
集合set操作命令
有序集合
Redis常用命令
在SpringBoot中使用redis
1.在springboot项目中导入redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在实现类中注入RedisTemplate
3.添加redis的配置类
添加redis的配置类是为了配置redis的序列化器,因为在执行完java程序后,在redis中存储的key会乱码,因此要设置key的序列化器,而对于value其实可以不设置,因为springboot在运行的时候会将数据序列化,在获取数据的时候会将数据反序列化。
package com.itheima.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//默认的Key序列化器为:JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
4.操作string类型的数据
//操作字符串类型
@Test
public void testString()
{
//设置值和取值
redisTemplate.opsForValue().set("city1234","郑州");
String value=(String) redisTemplate.opsForValue().get("city1234");
System.out.println(value);
//设置存活时间
redisTemplate.opsForValue().set("key1","1234",10L, TimeUnit.SECONDS);
//如果重复添加同一key的值,则会失败
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "上海");
System.out.println(aBoolean);
}
5.操作哈希类型的数据
/**
* 操作哈希类型的数据
* 哈希是基于字符串类型的key-value的映射表
* key映射表名 field字段 value值
*/
@Test
public void testHash()
{
//存入数据
redisTemplate.opsForHash().put("002","name","xiaoming");
redisTemplate.opsForHash().put("002","age","20");
redisTemplate.opsForHash().put("002","address","beijing");
//取数据
String age = (String)redisTemplate.opsForHash().get("002", "age");
System.out.println(age);
//获取hash表中的所有字段field
Set keys = redisTemplate.opsForHash().keys("002");
for(Object key:keys)
{
System.out.println(key);
}
//获取hash表中的所有值
List values = redisTemplate.opsForHash().values("002");
for(Object value:values)
{
System.out.println(value);
}
}
运行结果:
操作list类型的数据
//操作list类型的数据
@Test
public void testList()
{
//存值
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPush("mylist","a");
listOperations.leftPushAll("mylist","b","c",'d');
//取值
List<String> mylists = listOperations.range("mylist", 0l, -1l);
for(String value:mylists)
{
System.out.println(value);
}
//列表长度
Long len = listOperations.size("mylist");
System.out.println(len);
//出队列
int length = len.intValue();
for(int i=0;i<length;i++)
{
String element = (String) listOperations.rightPop("mylist");
System.out.println(element);
}
}
6.操作无序set集合数据
@Test
//操作set类型的数据
public void testSet()
{
SetOperations setOperations = redisTemplate.opsForSet();
//存值
setOperations.add("myset","a","b","c","d");
//取值
Set<String> myset = setOperations.members("myset");
for(String ele:myset)
{
System.out.println(ele);
}
//删除成员
setOperations.remove("myset","a","b");
}
7.操作有序集合ZSet
//操作有序集合ZSet
@Test
public void testZSet()
{
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//存值
zSetOperations.add("myzset","a",10.0);
zSetOperations.add("myzset","b",11.0);
zSetOperations.add("myzset","c",12.0);
zSetOperations.add("myzset","d",13.0);
zSetOperations.add("myzset","e",14.0);
zSetOperations.add("myzset","f",15.0);
//取值
Set<String> myzset = zSetOperations.range("myzset", 0, -1);
for (String s:myzset)
{
System.out.println(s);
}
//修改分数
zSetOperations.incrementScore("myzset","b",20);
//删除成员
zSetOperations.remove("myzset","a","c");
}
8.通用操作
//通用操作
@Test
public void testCommon()
{
//获取所有的key
Set keys = redisTemplate.keys("*");
for(Object key:keys)
{
System.out.println(key);
}
//判断某个key是否存在
Boolean itcast = redisTemplate.hasKey("itcast");
System.out.println(itcast);
//删除指定key
redisTemplate.delete("myzset");
//获取指定key对应的value的数据类型
DataType myset = redisTemplate.type("myset");
System.out.println(myset.name());
}