java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > sky-take-out redis使用

sky-take-out项目中Redis的使用示例详解

作者:iam_leeqing

SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现Redis的高级功能(如数据结构、事务、分布式锁),本文给大家介绍sky-take-out项目中Redis的使用,感兴趣的朋友一起看看吧

Spring Cache 是 Spring Framework 提供的一个抽象层,用于简化应用程序中的缓存管理。它允许开发者以声明式的方式将缓存添加到应用中,而不需要手动编写冗长的缓存逻辑代码。Spring Cache 通过提供一系列注解和配置选项,使得集成缓存变得非常简单。将 Redis 集成到 Spring 应用程序中,并将其作为 Spring Cache 的缓存提供者。这样做的好处是可以在开发过程中专注于业务逻辑的实现,只需通过 Spring Cache 提供的简单注解即可完成复杂的缓存管理任务。与此同时,你还能享受到 Redis 提供的高效性能和丰富的功能特性。这种方法不仅提高了开发效率,还增强了应用的可扩展性和性能表现。

Spring Cache主要特性

  1. 声明式缓存:通过使用注解(如 @Cacheable, @CachePut, @CacheEvict 等),可以非常方便地在方法上定义缓存行为。
  2. 多种缓存实现支持:Spring Cache 抽象层支持多种缓存解决方案,包括但不限于 Ehcache, Hazelcast, Infinispan, JCache (JSR-107), Guava Cache, Caffeine, Redis 等。
  3. 灵活的配置:可以通过 XML 配置、Java Config 或者自动配置来设置缓存管理器(Cache Manager)。
  4. 条件缓存:可以根据特定条件决定是否执行缓存操作(例如,使用 condition 属性)。
  5. SpEL 支持:可以在注解中使用 Spring Expression Language (SpEL) 来动态指定缓存键或其他属性值。

核心注解

1.@Cacheable

此注解通常用于查询操作,当调用被注解的方法时,如果缓存中存在对应的数据,则直接返回缓存结果,而不是执行方法体。

    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    @Cacheable(cacheNames = "setmealCache",key = "#categoryId")
    public Result<List<Setmeal>> list(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);
        List<Setmeal> list = setmealService.list(setmeal);
        return Result.success(list);
    }

2.@CachePut

@Cacheable 不同,@CachePut 总是会执行方法,并将方法的结果放入缓存中。适用于更新操作。

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    @CachePut(cacheNames = "setmealCache",key = "#categoryId")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }

3.@CacheEvict

用于清除缓存,适用于删除或无效化数据的操作。

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public Result startOrStop(@PathVariable Integer status, Long id) {
    setmealService.startOrStop(status, id);
    return Result.success();
}

@CacheEvict(cacheNames = "setmealCache", allEntries = true) 被用来清除名为 setmealCache 的缓存中的所有条目。

不是所有 Redis 的功能都可以通过 Spring Cache 注解实现。

✅Spring Cache 注解能做什么?

Spring Cache 的设计初衷是简化缓存操作,提供一种统一的、声明式的缓存管理方式,适用于常见的缓存场景,比如:

这些注解非常适合用于:

❌Spring Cache 注解不能做什么?

虽然 Spring Cache 很方便,但它是一个抽象层,只封装了缓存操作中最常见的部分。它并不能覆盖 Redis 所有的强大功能。以下是一些 Spring Cache 注解无法实现难以实现的功能:

Redis 功能Spring Cache 注解是否支持
字符串、哈希、列表、集合、有序集合等数据结构操作❌ 不支持,需使用 RedisTemplateStringRedisTemplate
发布/订阅消息(Pub/Sub)❌ 不支持
Lua 脚本执行❌ 不支持
分布式锁(SETNX / RedLock)❌ 不支持
HyperLogLog、Geo、Bitmap 等高级数据结构❌ 不支持
批量操作(Pipeline)❌ 不支持
事务(MULTI / EXEC)❌ 不支持
TTL、KEYS、SCAN 等管理命令❌ 不支持
自定义缓存过期策略(比如不同 key 有不同的 TTL)⚠️ 部分支持,但不够灵活

🛠️举个例子:

场景:缓存用户信息

@Cacheable("user")
public User getUserById(Long id) {
    return userRepository.findById(id);
}

✅ 适合用 Spring Cache,因为这是个典型的“查数据库缓存结果”的场景。

场景:实现一个分布式锁

Boolean success = redisTemplate.opsForValue().setIfAbsent("lock_key", "locked", 30, TimeUnit.SECONDS);

❌ 无法用 Spring Cache 实现,必须用 RedisTemplate 直接操作 Redis。

✅什么时候用 Spring Cache?

❌什么时候要直接使用 RedisTemplate?

✅最佳实践:结合使用

在实际项目中,推荐结合使用 Spring Cache 和 RedisTemplate

🧠 总结一句话:

Spring Cache 注解只能覆盖 Redis 的部分缓存功能,不能替代 Redis 的全部能力。
对于复杂的 Redis 操作,仍需使用 RedisTemplate

如果正在开发一个中大型项目,建议:

这样既能享受注解带来的便捷,又能保留 Redis 的灵活性和强大功能。

1. 字符串、哈希、列表、集合、有序集合等数据结构操作

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplateStringRedisTemplate 来进行更复杂的 Redis 数据类型操作。

示例:字符串操作

@Autowired
private StringRedisTemplate stringRedisTemplate;
public void stringOperationsExample(String key, String value) {
    // 设置字符串值
    stringRedisTemplate.opsForValue().set(key, value);
    // 获取字符串值
    String retrievedValue = stringRedisTemplate.opsForValue().get(key);
    System.out.println("Retrieved Value: " + retrievedValue);
}

示例:哈希操作

public void hashOperationsExample(String key, String field, String value) {
    // 设置哈希表中的字段值
    stringRedisTemplate.opsForHash().put(key, field, value);
    // 获取哈希表中字段的值
    Object fieldValue = stringRedisTemplate.opsForHash().get(key, field);
    System.out.println("Field Value: " + fieldValue);
}

示例:列表操作

public void listOperationsExample(String key, String... values) {
    // 右侧插入元素到列表
    for (String value : values) {
        stringRedisTemplate.opsForList().rightPush(key, value);
    }
    
    // 获取列表中的所有元素
    List<String> elements = stringRedisTemplate.opsForList().range(key, 0, -1);
    
    System.out.println("List Elements: " + elements);
}

示例:集合操作

public void setOperationsExample(String key, String... members) {
    // 添加成员到集合
    for (String member : members) {
        stringRedisTemplate.opsForSet().add(key, member);
    }
    // 获取集合中的所有成员
    Set<String> membersSet = stringRedisTemplate.opsForSet().members(key);
    System.out.println("Members of Set: " + membersSet);
}

示例:有序集合操作

public void sortedSetOperationsExample(String key, String member, double score) {
    // 添加成员到有序集合并指定分数
    stringRedisTemplate.opsForZSet().add(key, member, score);
    // 获取有序集合中的所有成员及其分数
    Set<ZSetOperations.TypedTuple<String>> tuples = stringRedisTemplate.opsForZSet().rangeWithScores(key, 0, -1);
    tuples.forEach(tuple -> System.out.println("Member: " + tuple.getValue() + ", Score: " + tuple.getScore()));
}

2. 发布/订阅消息(Pub/Sub)

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 进行发布/订阅操作。

@Autowired
private StringRedisTemplate stringRedisTemplate;
// 订阅者
public void subscribeToChannel(String channelName) {
    stringRedisTemplate.getConnectionFactory().getConnection().subscribe((message, pattern) -> {
        System.out.println("Received message on channel [" + new String(pattern) + "] with content: " + new String(message.getBody()));
    }, channelName.getBytes());
}
// 发布者
public void publishToChannel(String channelName, String message) {
    stringRedisTemplate.convertAndSend(channelName, message);
}

3. Lua 脚本执行

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 执行 Lua 脚本。

@Autowired
private StringRedisTemplate stringRedisTemplate;
public void executeLuaScript() {
    DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
    redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua_script.lua")));
    redisScript.setResultType(Long.class);
    List<String> keys = Arrays.asList("key1", "key2");
    Long result = stringRedisTemplate.execute(redisScript, keys, "arg1");
    System.out.println("Lua Script Result: " + result);
}

4. 分布式锁(SETNX / RedLock)

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 实现分布式锁。

@Autowired
private StringRedisTemplate stringRedisTemplate;
public boolean acquireLock(String lockKey, long timeout) throws InterruptedException {
    long millisecondsTimeout = System.currentTimeMillis() + timeout;
    while (System.currentTimeMillis() < millisecondsTimeout) {
        Boolean success = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "locked", Duration.ofMinutes(1));
        if (Boolean.TRUE.equals(success)) {
            return true;
        }
        Thread.sleep(100); // 等待一段时间后重试
    }
    return false;
}
public void releaseLock(String lockKey) {
    stringRedisTemplate.delete(lockKey);
}

5. HyperLogLog、Geo、Bitmap 等高级数据结构

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 操作这些高级数据结构。

Geo 操作示例:

public void geoOperationsExample() {
    stringRedisTemplate.opsForGeo().add("cities",
        new Point(13.361389, 38.115556), "Palermo",
        new Point(15.087269, 37.502669), "Catania");
    Distance distance = stringRedisTemplate.opsForGeo().distance("cities", "Palermo", "Catania", RedisGeoCommands.DistanceUnit.KILOMETERS);
    System.out.println("Distance between Palermo and Catania: " + distance.getValue() + " km");
}

6. 批量操作(Pipeline)

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 进行批量操作。

public void pipelineOperationsExample() {
    List<Object> results = stringRedisTemplate.executePipelined((RedisCallback<String>) connection -> {
        connection.openPipeline();
        connection.set("key1".getBytes(), "value1".getBytes());
        connection.incr("key2".getBytes());
        return null;
    });
    System.out.println("Pipeline Results: " + results);
}

7. 事务(MULTI / EXEC)

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 进行事务操作。

public void transactionOperationsExample() {
    stringRedisTemplate.execute(new SessionCallback<List<Object>>() {
        @Override
        public List<Object> execute(RedisOperations operations) throws DataAccessException {
            operations.multi();
            operations.opsForValue().increment("key1");
            operations.opsForValue().increment("key2");
            return operations.exec(); // 执行并返回结果
        }
    });
}

8. TTL、KEYS、SCAN 等管理命令

Spring Cache 注解的支持情况:

解决方案:
使用 RedisTemplate 设置 TTL 和执行其他管理命令。

public void ttlOperationsExample(String key) {
    // 设置TTL
    stringRedisTemplate.expire(key, Duration.ofMinutes(10));
}
public void scanKeysExample() {
    ScanOptions options = ScanOptions.scanOptions().match("*").count(100).build();
    Cursor<Map.Entry<byte[], byte[]>> cursor = stringRedisTemplate.executeWithStickyConnection(
        redisConnection -> new ConvertingCursor<>(redisConnection.scan(options),
            entry -> new AbstractMap.SimpleEntry<>(new String(entry.getKey()), new String(entry.getValue()))));
    while (cursor.hasNext()) {
        Map.Entry<String, String> entry = cursor.next();
        System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
    }
}

通过上述例子可以看出,虽然 Spring Cache 提供了便捷的方式来管理缓存,但对于 Redis 的复杂功能,需要依赖 RedisTemplateStringRedisTemplate 来实现。这种方式不仅保留了 Redis 的灵活性,同时也使得开发者能够充分利用 Redis 的强大功能。

到此这篇关于sky-take-out项目中Redis的使用的文章就介绍到这了,更多相关sky-take-out redis使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文