苍穹外卖项目学习日记(7) day05-1
Windows-Redis安装与运行
- 直接解压day05下的zip压缩文件,在解压目录下进入cmd,运行
redis-server.exe redis.windows.conf
即可运行成功
- 如果出现
Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功完成。
,表示端口被占用,需要结束占用端口的程序
- 结束程序:进入管理员cmd,
netstat -ano | findstr 6379
,得到对应的PID后,就可以直接taskkill -PID 得到的pid -F
,结束程序。也可以使用tasklist | findstr 得到的pid
来查看是什么程序占用了端口
- 运行
redis-cli.exe
即可进入redis的操作界面,还可以输入redis-cli.exe -h ip地址 -p 端口号
可以远程访问其他主机
- 增加密码:打开
redis.windows.conf
文件,搜索requirepass
,后面跟的就是密码,修改成自己密码即可,并且取消注释,后续客户端需要连接,需要加上-a+密码参数,例如:redis-cli.exe -h ip地址 -p 端口号 -a 密码
- 安装文件夹中另外一个软件,为redis的图形化界面
Redis常用命令
- redis常用数据类型
字符串操作
哈希操作
列表操作
集合操作
有序集合
常用命令
JAVA中操作Redis
-
spring data redis使用
-
导入坐标(项目中已经导入)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置数据源,放在spring下一级,即和datasource同级
- application.yml
redis:
host: ${sky.redis.host}
port: ${sky.redis.port}
database: ${sky.redis.database}
password: ${sky.redis.password}
redis:
host: localhost
port: 6379
password: 123456
database: 0
- 创建配置类RedisConfiguration
- RedisConfiguration.java
package com.sky.config;
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;
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板对象。。。。");
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
- 创建测试类,操作redis数据
- SpringDataRedisTest.java
package com.sky.test;
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.connection.DataType;
import org.springframework.data.redis.core.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class SpringDataRedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedisTemplate(){
System.out.println(redisTemplate);
ValueOperations valueOperations = redisTemplate.opsForValue();
HashOperations hashOperations = redisTemplate.opsForHash();
ListOperations listOperations = redisTemplate.opsForList();
SetOperations setOperations = redisTemplate.opsForSet();
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
}
@Test
public void testString(){
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("city","西宁");
String city = (String) valueOperations.get("city");
valueOperations.set("code","1234",3, TimeUnit.MINUTES);
valueOperations.setIfAbsent("lock","1");
valueOperations.setIfAbsent("lock","2");
System.out.println(city);
}
@Test
public void testHash(){
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("100","name","tjx");
hashOperations.put("100","age","23");
String name = (String) hashOperations.get("100", "name");
System.out.println(name);
Set keys = hashOperations.keys("100");
System.out.println(keys);
List values = hashOperations.values("100");
System.out.println(values);
hashOperations.delete("100","age");
}
@Test
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("mylist","a","b","c");
listOperations.leftPush("mylist","d");
List mylist = listOperations.range("mylist", 0, -1);
System.out.println(mylist);
listOperations.rightPop("mylist");
Long size = listOperations.size("mylist");
System.out.println(size);
}
@Test
public void testSet(){
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c","d");
setOperations.add("set2","a","b","x","y");
Set set1 = setOperations.members("set1");
System.out.println(set1);
Long set11 = setOperations.size("set1");
System.out.println(set11);
Set intersect = setOperations.intersect("set1", "set2");
System.out.println(intersect);
Set union = setOperations.union("set1", "set2");
System.out.println(union);
setOperations.remove("set1","a","b");
}
@Test
public void testZSet(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1","a",10);
zSetOperations.add("zset1","b",12);
zSetOperations.add("zset1","c",14);
Set zset1 = zSetOperations.range("zset1", 0, -1);
System.out.println(zset1);
zSetOperations.incrementScore("zset1","c",10);
zSetOperations.remove("zset1","a","b");
}
@Test
public void testCommon(){
Set keys = redisTemplate.keys("*");
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
redisTemplate.delete("mylist");
}
}