Redis 基本操作

字符串(Strings)、哈希(hashes)、列表(lists)、集合(sets)、有序集合(sorted sets)

🤩Dos操作Redis

redis-cli 连接Redis

Redis 基本操作

select index 切换库(Redis内置16个库

Redis 基本操作

flushdb 刷新库

Redis 基本操作

字符串

set key value [EX seconds] [PX milliseconds] [NX|XX] 存入数据
get key 取出数据
incr key 对数据进行加操作
decr key 对数据进行减操作

Redis 基本操作

哈希

hset key field value 存入数据
hget key field 取出数据

Redis 基本操作

列表

lpush key value [value …] 从左侧向列表中传值(可一次传多个
llen key 查看列表的长度
lindex key index 根据索引取值
lrange key start stop 根据范围取值
rpop key 弹出最右边的值

Redis 基本操作

集合

sadd key member [member …] 向集合中添加元素
scard key 集合中元素的个数
spop key [count] 随机弹出集合中的元素
smembers key 显示集合中的元素

Redis 基本操作

有序集合

有序集合就是给集合的每个元素加了个分数(double)来实现有序的,所以他的命令和集合相似

zadd key [NX|XX] [CH] [INCR] score member [score member …] 向有序集合中添加元素和他的分数
zcard key 集合中元素的个数
zscore key member 指定元素的分数
zrank key member 指定元素的排名(从小到大,从0开始
zrange key start stop [WITHSCORES] 取出范围内的值(从小到大排序

Redis 基本操作

全局命令

keys pattern 按pattern的要求去查询库里的key
type key 查询key的数据存储类型
exists key [key …] 查询库里是否存在key
del key [key …] 删除key

Redis 基本操作

expire key seconds 设置key的过期时间

Redis 基本操作

😠Spring整合Redis

引入依赖

 <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-redis</artifactid>
 </dependency>

配置Redis

RedisProperties
spring.redis.database = 11
spring.redis.host = localhost
spring.redis.port = 6379
package com.oia.community.config;

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.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<string,object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<string, object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // &#x8BBE;&#x7F6E;key&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;
        template.setKeySerializer(RedisSerializer.string());
        // &#x8BBE;&#x7F6E;value&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;
        template.setValueSerializer(RedisSerializer.json());
        // &#x8BBE;&#x7F6E;hash&#x7684;key&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;
        template.setHashKeySerializer(RedisSerializer.string());
        // &#x8BBE;&#x7F6E;hash&#x7684;value&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;
        template.setHashValueSerializer(RedisSerializer.json());

        template.afterPropertiesSet();
        return template;
    }
}

</string,></string,object>

访问Redis

字符串

 @Test
    public void testStrings() {
        String redisKey = "test:count";
        redisTemplate.opsForValue().set(redisKey, 1);
        System.out.println(redisTemplate.opsForValue().get(redisKey));
        System.out.println(redisTemplate.opsForValue().increment(redisKey));
        System.out.println(redisTemplate.opsForValue().decrement(redisKey));
    }

Redis 基本操作

哈希

 @Test
    public void testHash() {
        String redisKey = "test:user";
        redisTemplate.opsForHash().put(redisKey, "id", 1);
        redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");
        System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));
        System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));
    }

Redis 基本操作

列表

@Test
    public void testLists() {
        String redisKey = "test:ids";
        redisTemplate.opsForList().leftPush(redisKey, 101);
        redisTemplate.opsForList().leftPush(redisKey, 102);
        redisTemplate.opsForList().leftPush(redisKey, 103);

        System.out.println(redisTemplate.opsForList().size(redisKey));
        System.out.println(redisTemplate.opsForList().index(redisKey, 0));
        System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
    }

Redis 基本操作

集合

    @Test
    public void testSets() {
        String redisKey = "test:teachers";
        redisTemplate.opsForSet().add(redisKey, "&#x5218;&#x5907;", "&#x5173;&#x7FBD;", "&#x5F20;&#x98DE;", "&#x8D75;&#x4E91;", "&#x8BF8;&#x845B;&#x4EAE;");
        System.out.println(redisTemplate.opsForSet().size(redisKey));
        System.out.println(redisTemplate.opsForSet().pop(redisKey));//&#x968F;&#x673A;&#x5F39;&#x51FA;
        System.out.println(redisTemplate.opsForSet().members(redisKey));
    }

Redis 基本操作

有序集合

@Test
    public void testSortedSets() {
        String redisKey = "test:students";

        redisTemplate.opsForZSet().add(redisKey, "&#x5510;&#x50E7;", 80);
        redisTemplate.opsForZSet().add(redisKey, "&#x609F;&#x7A7A;", 90);
        redisTemplate.opsForZSet().add(redisKey, "&#x516B;&#x6212;", 50);
        redisTemplate.opsForZSet().add(redisKey, "&#x6C99;&#x50E7;", 70);
        redisTemplate.opsForZSet().add(redisKey, "&#x767D;&#x9F99;&#x9A6C;", 60);

        System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
        System.out.println(redisTemplate.opsForZSet().score(redisKey, "&#x516B;&#x6212;"));
        System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "&#x516B;&#x6212;"));
        System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));
    }

Redis 基本操作

key

 @Test
    public void testkeys() {
        redisTemplate.delete("test:user");
        System.out.println(redisTemplate.hasKey("test:user"));
        redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);
    }

Redis 基本操作

简化方案(多次访问同一个key)

 @Test
    public void testBoundOperations(){
        String redisKey = "test:count";
        BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);
        operations.increment();
        operations.increment();
        operations.increment();
        operations.increment();
        operations.increment();
        System.out.println(operations.get());
    }

Redis 基本操作

事务

Redis查询时必须待队列中的事务提交后才出结果,因此一般使用编程式事务

@Test
    public void testTransactional(){
        Object obj = redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String redisKey = "test:tx";
                operations.multi();//&#x542F;&#x7528;&#x4E8B;&#x52A1;
                operations.opsForSet().add(redisKey,"&#x5F20;&#x4E09;");//&#x4F1A;&#x8FD4;&#x56DE;&#x5F71;&#x54CD;&#x884C;&#x6570;
                operations.opsForSet().add(redisKey,"&#x674E;&#x56DB;");
                operations.opsForSet().add(redisKey,"&#x738B;&#x4E94;");
                System.out.println(operations.opsForSet().members(redisKey));
                // &#x5728;redis&#x7BA1;&#x7406;&#x4E8B;&#x52A1;&#x65F6;&#x4E2D;&#x95F4;&#x4E0D;&#x8981;&#x505A;&#x67E5;&#x8BE2;
                return operations.exec();//&#x63D0;&#x4EA4;&#x4E8B;&#x52A1;
            }
        });
        System.out.println(obj);
    }

Redis 基本操作
  • 前三个显示0是因为之前已经插入过啦,set不重复,所以这次插入失败

Original: https://www.cnblogs.com/xiannveryao/p/16724738.html
Author: nnn~
Title: Redis 基本操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/604657/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球