Redis
介绍:
Redis是一个基于内存的key-value结构数据库(MySQL是通过数据文件方式存储在磁盘上,数据结构是二维表)
特点:
更改配置文件:
使用密码:
redis默认是不需要密码的,如果要设置密码,需要:打开redis.windows.conf,搜索pass (加一个空格),找到这个部分:# requirepass foobared,foobared部分改为你想设置的密码,然后去掉注释#,就可以了。
使用:
启动服务
在当前下载redis的文件路径下打开cmd,输入redis-server.exe redis.windows.conf,回车
启动服务后,可以选择两种方式进行连接
连接:
cmd方式:
在这个文件路径下再打开一个cmd,注意上面打开那个cmd不要关,然后输入redis-cli.exe,这里返回127.0.0.1:6379就说明连上了
图形界面方式:
下载图形化界面Another Redis Desktop Manager,新建连接,输入配置信息,进行连接
常用数据类型:
各种数据类型的特点:
常用命令:
字符串操作命令:
哈希操作命令:
列表操作命令:
集合操作命令:
有序集合操作命令:
通用命令:
操作Redis:
想要了解Redis的基础知识及使用方法可以参考作者另一边文章《Java_中间件——Redis》
Redis的Java客户端:
下面我们使用Spring Data Redis来操作Redis
Spring Data Redis使用方式:
配置类RedisTemplate:
@Configuration
@Slf4j
public class RedisConfiguration {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {log.info("开始创建redis模板对象...");RedisTemplate redisTemplate = new RedisTemplate();//设置redis连接工厂对象redisTemplate.setConnectionFactory(redisConnectionFactory);//设置redis key的序列化器redisTemplate.setKeySerializer(new StringRedisSerializer());return redisTemplate;}}
代码演示:
@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate() {System.out.println(redisTemplate);//操作字符串ValueOperations valueOperations = redisTemplate.opsForValue();//操作HashHashOperations hashOperations = redisTemplate.opsForHash();//操作ListListOperations listOperations = redisTemplate.opsForList();//操作setSetOperations setOperations = redisTemplate.opsForSet();//操作有序集合ZSetOperations zSetOperations = redisTemplate.opsForZSet();}/*** 操作字符串类型的数据*/@Testpublic void testStirng() {// set get setex setnxredisTemplate.opsForValue().set("city","北京");String city = (String) redisTemplate.opsForValue().get("city");System.out.println(city);redisTemplate.opsForValue().set("code","6562",60, TimeUnit.SECONDS);redisTemplate.opsForValue().setIfAbsent("lock","1");redisTemplate.opsForValue().setIfAbsent("lock","2");}/*** 操作哈希类型的数据*/@Testpublic void testHash() {//hset hget hdel hkeys hvalsHashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("100","name","xiaobai");hashOperations.put("100","age","20");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");}/*** 操作列表类型的数据*/@Testpublic void testList() {// lpush lrange rpop llenListOperations 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);}/*** 操作集合类型的数据*/@Testpublic void testSet() {// sadd smembers scard sinter sunion sremSetOperations setOperations = redisTemplate.opsForSet();setOperations.add("set1","a","b","c");setOperations.add("set2","b","c","d");Set set1 = setOperations.members("set1");System.out.println(set1);Long size = setOperations.size("set1");System.out.println(size);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");}/*** 操作有序集合类型的数据*/@Testpublic void testZSet() {// zadd zrange zincrby zremZSetOperations zSetOperations = redisTemplate.opsForZSet();zSetOperations.add("zset1","a",10);zSetOperations.add("zset1","b",11);zSetOperations.add("zset1","c",9);Set zset1 = zSetOperations.range("zset1", 0, -1);System.out.println(zset1);zSetOperations.incrementScore("zset1","c",10);zSetOperations.remove("zset1","a","b");}/*** 通用命令操作*/@Testpublic void testCommon() {// keys exists type delSet 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");}