<p> 今天咱来聊一下Redis五种数据类型的详细用法以及在代码中如何使用。废话不多说,开始!</p><p>
</p><p>Redis五种数据类型:
</p><p>(1)string:字符串对象
</p><p>(2)list:列表对象</p><p>(3)hash:散列
</p><p>(4)set:集合</p><p>(5)zset:有序集合</p><p>
</p><p>接下来一一进行操作(命令行操作,由于很多,就不一一截图了):
</p><p><strong>一、统一操作(五种数据类型都拥有,针对key)</strong></p><p>我给定一个key:k1</p><pre>//获取所有key
keys *
//删除k1
DEL k1
//检查给定的k1是否存在
EXISTS k1
//给k1设置1秒的过期时间,1秒过后,k1被删除
EXPIRE k1 1
//给k1设置过期时间为unix时间戳,也就是北京时间2019-11-29 22:22:09
EXPIREAT k1 1575037329
//查看k1还有多长时间过期,单位为毫秒
PTTL k1
//查看k1还有多长时间过期,单位为秒
TTL k1
//取消k1的过期时间设置
PERSIST k1
//修改key的名称,将k1修改为k2
RENAME k1 k2
//查看k1是什么数据类型的
TYPE K1</pre><p>
</p><p><strong>二、string(字符串对象)</strong></p><pre>//设置k1的值为value1
set k1 value1
//获取k1的值
get k1
//先获取k1的值,然后再将k1的值设为k1_value
GetSet k1 k1_value
//同时设置多个key的值,k1的值为v1,k2的值为v2
mset k1 v1 k2 v2
//同时获取k1,k2的值
mget k1 k2
//将k1的值加1
incr k1
//将k1的值加上3
incrby k1 3
//将k1的值减1
decr k1
//将k1的值减3
decrby k1 3
//在k1的值后面添加相应的字符串
append k1 value</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-f4332a9fd975b2d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p><strong>三、list</strong><strong>(列表对象)</strong></p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-7116487abf47d2ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>从左到右索引为0,1,2...</p><pre>//将v1,v2,v3添加到列表k1
LPUSH k1 v1 v2 v3
//获取列表k1中索引为1的值,索引从0算起
LINDEX k1 1
//获取列表k1的元素个数
LLEN k1
//移除并获取k1的第一个元素
LPOP k1
//获取索引0~2的元素,当2改成-1,则获取所有元素
LRANGE k1 0 2
//设置索引为1的元素值为v4
LSET k1 1 v4
//移除并获取列表k1的最后一个元素
RPOP k1
//在列表k1尾部添加v5和v6两个元素
RPUSH k1 v5 v6</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-e46dbf4f5ef0d10d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p><strong>四、hash(散列对象)</strong>
</p><p>散列对象相当于字典(map)</p><pre>//给h_k1添加一个键为k1,值为v1的entry
HSET h_k1 k1 v1
//获取h_k1中k1的值
HGET h_k1 k1
//删除h_k1中的k1,可以删除多个,key之间空格隔开
HDEL h_k1 k1
//获取h_k1中k1的值
HGET h_k1 k1
//给h_k1添加<k2,v2>,<k3,v3>的entry
HMSET h_k1 k2 v2 k3 v3
//同时获取h_k1中k2,k3的值
HMGET h_k1 k2 k3
//获取h_k1中的所有键对值
HGETALL h_k1
//获取h_k1中的键对值数量
HLEN h_k1
//获取h_k1中所有的key
HKEYS h_k1
//获取h_k1中所有的value
HVALS h_k1
//查看h_k1中key为k2的entry是否存在
HEXISTS h_k1 k2</pre><p>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-2e945a5d426d43c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p><strong>五、set(集合)</strong></p><p>集合中的元素是唯一的,类似于java中的set集合</p><pre>//往s_k1集合中添加v1,v2,v3,v4
SADD s_k1 v1 v2 v3 v4
//获取s_k1集合成员数量
SCARD s_k
//获取s_k1中的所有成员
SMEMBERS s_k1
//移除并返回s_k1中随机一个数
SPOP s_k1
//给s_k1,s_k2做交集
SINTER s_k1 s_k2
//给s_k1,s_k2做交集并且将结果存储到s_k3
SINTERSTORE s_k3 s_k1 s_k2
//给s_k1,s_k2做并集
SUNION s_k1 s_k2
//给s_k1,s_k2做并集并且将结果存储到s_k4
SUNIONSTORE s_k4 s_k1 s_k2</pre><p>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-504bc30b26629da6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p><strong>六、zset(有序集合)</strong></p><pre>//给z_k1添加三个元素v1、v2、v3,分值分别是1、2、3
ZADD z_k1 1 v1 2 v2 3 v3
//获取z_k1的元素个数
ZCARD z_k1
//获取分值为1~3的成员数
ZCOUNT z_k1 1 3
//获取索引 0~2的成员,2改成-1为获取所有成员
ZRANGE z_k1 0 2
//获取索引 0~2的成员以及其分值
ZRANGE z_k1 0 2 withscores
//获取分值为0~2的成员,后面也可以带上withscores
ZRANGEBYSCORE z_k1 0 2
//获取v2的索引
ZRANK z_k1 v2
//获取z_k1中v1的分值
ZSCORE z_k1 v1
//对z_k1和z_k2做交集,中间的2表示对两个有序集合做交集,并将结果存在z_k3
ZINTERSTORE z_k3 2 z_k1 z_k2
//对z_k1和z_k2做并集,中间的2表示对两个有序集合做并集,并将结果存在z_k4
ZUNIONSTORE z_k4 2 z_k1 z_k2</pre><p>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-72198ce44198bb4a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p>
</p><p>命令行的常规操作如上,接下来看一下代码(分为普通maven项目和 spring boot项目)如何操作。
</p><p><strong>一、普通maven项目</strong>
</p><p>(1)操作单机redis(一般使用jedis连接,需要导入jedis的jar包)</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-3ed17f7863b8ec32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p><strong>:</strong>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-7df90489bda1c35e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>连接之后就可以直接用返回的jedis操作api,相应的IDEA都会有提示,这里就不在赘述。</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-901ea3afbf923a04.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>(2)连接集群方式</p><p>连接无密码集群:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-2520f6fab80c5814.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>连接有密码集群:
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-8e4f6c95c5df4399.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p><strong>二、springboot上操作redis</strong></p><p>首先得在pom文件中添加依赖:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-ba4bb09a9fd93cbe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>(1)单机版
</p><p>spring-data-redis提供了一种代码配置(需要利用@Configuration注解创建一个配置类)的方式可获取RedisTemplate
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-3b38bc523233ad7a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>配置之后就可用直接用@Autowired注解注入进行使用。
</p><p>
</p><p>
</p><p>也可通过配置文件(application.properties)配置redis信息进行连接</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-5c566b587d88e06e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>通过配置文件可以直接在代码中通过注解注入的方式使用</p><pre>@Autowired
StringRedisTemplate stringRedisTemplate;
//自定义初始化之后执行的方法
@PostConstruct
public void run() throws Exception {
ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
stringStringValueOperations.set("k1","v1");
System.out.println(stringStringValueOperations.get("k1"));
}</pre><p>
</p><p>
</p><p>(2)集群连接方式</p><p>配置文件</p><pre>#数据库
spring.redis.database=0
spring.redis.password=123456
设置为0可以让master挂机后,直接切换到slave
spring.redis.cluster.max-redirects=0
spring.redis.cluster.nodes=192.168.197.100:7001,192.168.197.110:7001,192.168.197.120:7001,192.168.197.100:7002,192.168.197.110:7002,192.168.197.120:7002
spring.redis.jedis.pool.max-wait=3600
spring.redis.jedis.pool.max-active=1
spring.redis.jedis.pool.max-idle=1
spring.redis.jedis.pool.min-idle=1</pre><p>
</p><p>配置类:</p><pre>package com.liusy.zuul.com;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Configuration
public class redisConfig {
//获取配置文件里的值
@Value("{spring.redis.cluster.nodes}")
String nodes;
@Value("{spring.redis.jedis.pool.max-wait}")
String maxWritTime;
@Value("{spring.redis.jedis.pool.max-idle}")
String maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
String minIdle;
@Bean
public RedisTemplate redisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(getConnectionFactory());
return redisTemplate;
}
@Bean
public RedisConnectionFactory getConnectionFactory() {
List<String> nodelist = new ArrayList<>();
nodelist = Arrays.asList(nodes.split(","));
RedisClusterConfiguration config = new RedisClusterConfiguration(nodelist);
config.setPassword(RedisPassword.of(password));
config.setMaxRedirects(Integer.parseInt(redirects));
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(config);
connectionFactory.afterPropertiesSet();
return connectionFactory;
}
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(Integer.valueOf(maxActive));
jedisPoolConfig.setMaxWaitMillis(Integer.valueOf(maxWritTime));
jedisPoolConfig.setMaxIdle(Integer.valueOf(maxIdle));
jedisPoolConfig.setMinIdle(Integer.valueOf(minIdle));
// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
jedisPoolConfig.setTestOnBorrow(true);
// 在空闲时检查有效性, 默认false
jedisPoolConfig.setTestWhileIdle(false);
return jedisPoolConfig;
}
}</pre><p>
</p><p>springboot启动之后直接用@Autowired注解注入RedisTemplate使用就可以了</p><p>
</p><p>以上就是redis的使用,全文比较粗糙,展开就会很长。下篇将聊一下Redis底层数据结构的实现。</p><p>
</p><p>=======================================================</p><p>我是Liusy,一个喜欢健身的程序员。</p><p>欢迎关注【Liusy01】,一起交流Java技术及健身,获取更多干货。</p><p>
</p>