Redis的valus值的五种数据类型
问题:Windows下出现Could not connect to Redis at 127.0.0.1:6379: 由于目标计算机积极拒绝,无法连接。
解决方法为在Redis-x64-3.2.100目录下打开两个cmd窗口,分别输入 命令redis-server.exe redis.windows.conf 和 命令redis-cli ,即可成功连接。
字符串string操作命令
哈希hash操作命令
列表list操作命令
集合set操作命令
通用命令
使用Spring Data Redis操作Redis
springboot集成Redis
- 首先在maven的pom文件中引入 Spring Data Redis 模块,代码如下。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 然后,在springboot的配置文件中写入redis配置,代码如下。
spring: application: name:redisdemo #Redis相关配置 redis: host: localhost port: 6379 #password: database: 0 jedis: #Redis 连接池配置 pool: max-active: 8 #最大连接数 max-idle: 4 #连接池中最大空闲连接 max-wait: 1ms #连接池最大阻塞等待时间 min-idle: 0 #连接池中最小空闲连接
- 然后,处理keys的序列化问题,config中创建配置文件,其大致内容是更换redis对keys的序列化方式,然后返回RedisTemplate对象,放入Spring容器当中,代码如下。
/** * 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; } }
- 最后,业务逻辑代码如下。
@SpringBootTest // SpringRunner 是 Spring Test 模块提供的一个测试运行器, // 它能够集成 Spring 框架和 JUnit,使得在测试过程中可以使用 Spring 的功能,如依赖注入、事务管理等。 @RunWith(SpringRunner.class) class RedisdemoApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test public void testString() { redisTemplate.opsForValue().set("city123", "beijing"); } }
Spring Data Redis对Redis的具体操作
操作String类型数据
@Test
public void testString(){
redisTemplate.opsForValue().set("city123","beijing");
String value = (String) redisTemplate.opsForValue().get("city123");
System.out.println(value);
redisTemplate.opsForValue().set("key1","value1",10l, TimeUnit.SECONDS);
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing");
System.out.println(aBoolean);
}
操作Hash类型数据
@Test
public void testHash(){
HashOperations hashOperations = redisTemplate.opsForHash();
//存值
hashOperations.put("002","name","xiaoming");
hashOperations.put("002","age","20");
hashOperations.put("002","address","bj");
//取值
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 value : values) {
System.out.println(value);
}
}
操作List类型的数据
@Test
public void testList(){
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(element);
}
}
操作Set类型的数据
@Test
public void testSet(){
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");
//取值
myset = setOperations.members("myset");
for (String o : myset) {
System.out.println(o);
}
}
操作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","a",13.0);
//取值
Set<String> myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
//修改分数
zSetOperations.incrementScore("myZset","b",20.0);
//取值
myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
//删除成员
zSetOperations.remove("myZset","a","b");
//取值
myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
}
通用操作,针对不同的数据类型都可以操作
@Test
public void testCommon(){
//获取Redis中所有的key
Set<String> keys = redisTemplate.keys("*");
for (String key : keys) {
System.out.println(key);
}
//判断某个key是否存在
Boolean itcast = redisTemplate.hasKey("itcast");
System.out.println(itcast);
//删除指定key
redisTemplate.delete("myZset");
//获取指定key对应的value的数据类型
DataType dataType = redisTemplate.type("myset");
System.out.println(dataType.name());
}