gpt4 book ai didi

java - Spring 使用 Redis @Cacheable

转载 作者:IT王子 更新时间:2023-10-29 06:01:05 26 4
gpt4 key购买 nike

任何人都可以为 Collection 类型推荐一个 Redis (NoSQL DB) 示例吗?通常我们使用以下(在 Spring 中):

@Cacheable(value = "PRODUCT", key = "#productId" ) 
public Map<String,Object> findProduct(String productId, String productName)
{ return map; }

它将键和值存储为字符串,但我需要:

public Map<RestaurantId,Set<Order>>find(String RestaurantId, String productName){ return map; }

最佳答案

确保您的对象是可序列化的,然后简单地使用 Spring Data Redis cache abstraction ( 2015 link )

从上面的文档中复制的示例:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true"/>

<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>

<!-- turn on declarative caching -->
<cache:annotation-driven />

<!-- declare Redis Cache Manager -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/>
</beans>

关于java - Spring 使用 Redis @Cacheable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com