您的位置:首页 > 数据库 > Redis

Spring Data Redis快速使用

2017-07-12 12:58 399 查看
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
@Resourc
4000
e(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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: