Spring Data Redis快速使用

来源:互联网 发布:惊雷算法 编辑:程序博客网 时间:2024/05/23 22:31

Spring Data Redis的快速使用
官网地址:http://projects.spring.io/spring-data-redis/

快速使用分三步:

1. 引用获取
The recommended way to get started using spring-data-redis in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.
初始使用 spring-data-redis,建议在你的应用中使用一个依赖管理系统,这样你就可以直接粘贴下面的内容到你的工程来使用。如果需要另外的帮助,请查看我们利用Maven 和 Gradle创建的指导。

<dependencies>    <dependency>        <groupId>org.springframework.data</groupId>        <artifactId>spring-data-redis</artifactId>        <version>1.8.4.RELEASE</version>    </dependency></dependencies>

2. 配置RedisTemplete

<bean id="jedisConnFactory"     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="jedisConnFactory"/>

3. 注入并使用 RedisTemplate 或其他 opsForX()实例

public class Example {    // inject the actual template    @Autowired    private RedisTemplate<String, String> template;    // inject the template as ListOperations    // can also inject as Value, Set, ZSet, and HashOperations    @Resource(name="redisTemplate")    private ListOperations<String, String> listOps;    public void addLink(String userId, URL url) {        listOps.leftPush(userId, url.toExternalForm());        // or use template directly        redisTemplate.boundListOps(userId).leftPush(url.toExternalForm());    }}
原创粉丝点击