Spring使用Redis @Cacheable

时间:2013-12-19 11:29:10

标签: java spring redis

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

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

将键和值存储为String,但我需要:

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

2 个答案:

答案 0 :(得分:4)

确保您的对象是可序列化的,然后只需使用Spring Data Redis cache abstraction2015 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>

答案 1 :(得分:1)

您也可以使用提供Redisson经理的Spring Cache框架。