21、整合Redis

  • 首先在pom文件中添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • 在yml中添加配置文件
spring:
  redis:
    database: 0
    host: 192.168.0.198
    port: 6379
    password: 123456
  • redis 操作类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisOperator {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // Key(键),简单的key-value操作

    /**
     * 对应 -TTL key,返回给定 key的剩余生存时间(TTL, time to live)。
     */
    public long ttl(String key) {
        return redisTemplate.getExpire(key);
    }

    /**
     * 对应-expire key seconds 设置过期时间,单位秒
     */
    public void expire(String key, long timeout) {
        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }
    
    /**
     * 对应-INCR key,增加key一次
     */
    public long incr(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 对应-KEYS pattern,查找所有符合给定模式 pattern的 key
     */
    public Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    /**
     * 对应-DEL key,删除一个key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    // String(字符串)

    /**
     * 对应-SET key value,设置一个key-value(将字符串值 value关联到 key)
     */
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 对应-SET key value EX seconds,设置key-value和超时时间(秒)
     */
    public void set(String key, String value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 对应-GET key,返回 key所关联的字符串值。
     */
    public String get(String key) {
        return (String)redisTemplate.opsForValue().get(key);
    }

    /**
     * 批量查询,对应mget [key,...]
     */
    public List<String> mget(List<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }

    /**
     * 批量查询,管道pipeline
     */
    public List<Object> batchGet(List<String> keys) {
        List<Object> result = redisTemplate.executePipelined((RedisCallback<String>) connection -> {
            StringRedisConnection src = (StringRedisConnection)connection;

            for (String k : keys) {
                src.get(k);
            }
            return null;
        });

        return result;
    }


    // Hash(哈希表)

    /**
     * 对应-HSET key field value,将哈希表 key中的域 field的值设为 value
     */
    public void hset(String key, String field, Object value) {
        redisTemplate.opsForHash().put(key, field, value);
    }

    /**
     * 对应-HGET key field,返回哈希表 key中给定域 field的值
     */
    public String hget(String key, String field) {
        return (String) redisTemplate.opsForHash().get(key, field);
    }

    /**
     * 对应-HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
     */
    public void hdel(String key, Object... fields) {
        redisTemplate.opsForHash().delete(key, fields);
    }

    /**
     * 对应-HGETALL key,返回哈希表 key中,所有的域和值。
     */
    public Map<Object, Object> hgetall(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    // List(列表)

    /**
     * 对应-LPUSH key value,将一个值 value插入到列表 key的表头
     */
    public long lpush(String key, String value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    /**
     * 对应-LPOP key,移除并返回列表 key的头元素。
     */
    public String lpop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 对应-RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。
     */
    public long rpush(String key, String value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }
}
  • 使用方法
@Api(tags = "Redis测验")
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisOperator redisOperator;

    @GetMapping("set")
    public Result set(String key,String value){
        redisOperator.set(key,value);
        return Result.success();
    }

    @GetMapping("get")
    public Result get(String key){
        return Result.success(redisOperator.get(key));
    }

    @GetMapping("del")
    public Result del(String key){
        redisOperator.del(key);
        return Result.success();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容