redis是一个基于内存的Key-Value数据库
1.基于内存存储,读写性能高
2.适合存储热点数据(热点商品、咨询、新闻)
3.NoSQL,不仅仅是SQL,泛指关系数据库
关系型数据库 Mysql Oracle DB2 SQLServer
非关系型数据库 Redis Mongo db MemCached
应用场景:缓存、任务队列、消息队列、分布式锁
一、下载
二、安装
windows安装配置,直接解压即可绿色文件
三、Redis的服务启动与关闭
1、linux启动与链接
使用./redis-cli 连接redis。
redis打开的服务在前台运行,霸屏了。所以让他在后台运行。
(1)找到redis.conf修改配置文件
(2)输入/dae查找修改的行
(3)将no改成yes
(4)使用redis运行带上redis.conf文件就可以在后台运行了
2、windows启动与链接
关闭:ctrl+c
四、linux和windows设置密码
1、Linux设置密码
(1)打开redis.conf
(2)输入\pass找到密码查询
(3)删除注释开启密码询问,并将后面修改为密码(root)
(4)关闭redis,重新打开
1、ps -ef | grep redis 2、kill -9 进程号 3、
(5)使用查询,是不可以用的,需要使用auth 密码才可以使用
(6)exit;是退出命令行
五、设置远程连接
(1)找到redis.conf文件进行修改
(2)将bind 127.0.0.1这行进行注释
(3)注意一定要打开防火墙
firewall-cmd --zone=public --add-port=6379/tcp --permanent(1)
firewall-cmd --reload(2)
(4)使用windows连接远程linux的redis
找到windows下安装的redis文件,输入如下命令便可以连接。注意文件目录
.\redis-cli.exe -h 192.168.52.137 -p 6379 -a root
六、redis数据类型
Redis存储的是key-value结构的数据,其中key是字符串类型,value有5种常用的数据类型
字符串String 哈希hash 列表list 集合set 有序集合sorted set
七、Redis常用命令
1、操作字符串String命令
2、操作哈希hash操作命令
3、列表list操作命令
4、集合set操作命令
5、通用命令
八、在java种操作Redis
了解:
springboot提供的重点:
pow.xml select 1进入1号数据库
注意要写入配置文件
package com.itheima.config;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
public class RedisConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
// 设置缓存有效期一小时
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1));
return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)).cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate();
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
}
(1)操作String类型数据
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void contextLoads() {
ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
opsForValue.set("name1", "lisi1"); // 缓存数据
opsForValue.set("name2", "lisi1",10L, TimeUnit.SECONDS); // 缓存数据,超时10秒就关闭
Boolean aBoolean = opsForValue.setIfAbsent("name1", "lisi1");// 缓存数据,判断是否存在,存在就添加失败
String value = opsForValue.get("name1"); // 获取缓存数据
System.out.println(aBoolean);
System.out.println(value);
}
(2)操作hash数据
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
HashOperations hashOperations = redisTemplate.opsForHash();
//存值
hashOperations.put("002","name","xiaoming");
hashOperations.put("002","age","20");
hashOperations.put("002","address","changsha");
//取值
String age =(String) hashOperations.get("002", "age");
System.out.println(age);
//获取hash中所有字段
Set keys = hashOperations.keys("002");
for (Object key:keys) {
System.out.println(key);
}
//获取hash中的所有值
List values = hashOperations.values("002");
for (Object key:values) {
System.out.println(key);
}
}
}
(3)操作List数据
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
ListOperations listOperations = redisTemplate.opsForList();
//存值
listOperations.leftPush("mylist", "a");
listOperations.leftPushAll("mylist", "b", "c", "d");
//取值
List<String> mylist = listOperations.range("mylist", 0, -1);
for (String value : mylist) {
System.out.println(value);
}
//获取列表的长度llen
Long size = listOperations.size("mylist");
int lSize = size.intValue();
for (int i = 0; i < lSize; i++) {
//出队列
String element = (String) listOperations.rightPop("mylist");
System.out.println(i + ":" + element);
}
}
(4)操作Set类型的数据
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
SetOperations setOperations = redisTemplate.opsForSet();
//存值
setOperations.add("myset","a","b","c","a");
//取值
Set<String> myset = setOperations.members("myset");
for (String o: myset) {
System.out.println(o);
}
//删除成员
setOperations.remove("myset","a","b");
System.out.println("***************");
//取值
Set<String> myset1 = setOperations.members("myset");
for (String o: myset1) {
System.out.println(o);
}
}
}
(5)操作Zset数据
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
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);
//取值
Set<String> myZset = zSetOperations.range("myZset", 0, -1);
for (String o:myZset) {
System.out.println(o);
}
//修改分数
zSetOperations.incrementScore("myZset","b",20.0);
//取值
Set<String> myZset1 = zSetOperations.range("myZset", 0, -1);
for (String o:myZset1) {
System.out.println(o);
}
//删除成员
zSetOperations.remove("myZset","a","b");
//取值
Set<String> myZset2 = zSetOperations.range("myZset", 0, -1);
for (String o:myZset2) {
System.out.println(o);
}
}
}
(6)通用方法
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
//获取Redis所有的keys
Set<String> keys = redisTemplate.keys("*");
for (String key:keys) {
System.out.println(key);
}
//判断某个key是否存在
Boolean itheima = redisTemplate.hasKey("itheima");
System.out.println(itheima);
//删除指定的key
redisTemplate.delete("myZset");
//获取指定key对应的value数据类型
DataType dataType = redisTemplate.type("myset");
System.out.println(dataType.name());
}
}