Redis是一種nosql資料庫,在開發中常用做緩存。Jedis是Redis在java中的redis- client.在此之前,希望已經了解redis的基本使用和Maven的使用。建立Maven Project之後,在POM.xml中添加jedis和spring-data-redis的依賴如下:
[html] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.0.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- spring-redis -->
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.0.RELEASE</version>
- </dependency>
Redis連接配接資料庫參數如下:applicationContext-redis.properties
[plain] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- #redis config
- redis.pool.maxActive=100
- redis.pool.maxIdle=20
- redis.pool.maxWait=1000
- redis.pool.testOnBorrow=true
- redis.hostname=localhost
- redis.port=6379
- redis.password=
在上下文配置中使用key-value讀取方式讀取properties中的值:
[html] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <!-- Jedis 連接配接池配置-->
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxActive" value="${redis.pool.maxActive}" />
- <property name="maxIdle" value="${redis.pool.maxIdle}" />
- <property name="maxWait" value="${redis.pool.maxWait}" />
- <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
- </bean>
- <!-- Jedis ConnectionFactory 資料庫連接配接配置-->
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="${redis.hostname}" />
- <property name="port" value="${redis.port}" />
- <property name="password" value="${redis.password}" />
- <property name="poolConfig" ref="jedisPoolConfig" />
- </bean>
- <!—- redisTemplate配置,redisTemplate是對Jedis的對redis操作的擴充,有更多的操作,封裝使操作更便捷 -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />
上面redisTemplate已經基本配置完成。
接下來建立User類,必須實作或者間接實作Serializable接口:
Redis存儲對象是使用序列化,spring-data-redis已經将序列化的功能内置,不需要我們去管,我們隻需要調用api就可以使用。SerialVersionUID字段對序列化擴充有用,為了以後擴充或者縮減字段時不會造成反序列化出錯。
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- public class User implements Serializable {
- private static final long serialVersionUID = -7898194272883238670L;
- public static final String OBJECT_KEY = "USER";
- public User() {
- }
- public User(String id) {
- }
- public User(String id, String name) {
- this.id = id;
- this.name = name;
- }
- private String id;
- private String name;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String toString() {
- return "User [id=" + id + ", name=" + name + "]";
- }
- public String getKey() {
- return getId();
- }
- public String getObjectKey() {
- return OBJECT_KEY;
- }
- }
建立userService類來操作redis增删查改緩存對象。
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- public class UserService {
- RedisTemplate<String, User> redisTemplate;
- public RedisTemplate<String, User> getRedisTemplate() {
- return redisTemplate;
- }
- public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- public void put(User user) {
- redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
- }
- public void delete(User key) {
- redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey());
- }
- public User get(User key) {
- return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey());
- }
- }
在上下文中配置redisTemplate注入,在使用bean方式來注入時,redisTemplate必須有setter/getter方法:
[html] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <bean id="userService" class="Service.UserService">
- <property name="redisTemplate">
- <ref bean="redisTemplate" />
- </property>
- </bean>
======如果使用注解方式自動注入,則可以注釋掉上面的bean配置方式======
在UserService注解@Service(“userService”),也可以在Service裡寫名字,預設是第一字母小寫。
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- @Service("userService")
- public class UserService {
- @Autowired
- RedisTemplate<String, User> redisTemplate;
- ……
- ……
- }
在上下文配置檔案中,添加自動掃描包的context節點,Base-package的路徑要覆寫包含注解的類檔案: [html] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- <context:component-scan base-package="*" />
在main中來簡單操作一下:
[java] view plain copy
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5Gcu82Yp9VRE90Qvw1c0V2czF2LcRXZu5ibkN3YuUGZvN2Lc9CX6MHc0RHaiojIsJye.png)
- public class Main {
- public static void main( String[] args )
- {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/conf/applicationContext.xml" );
- UserService userService = (UserService) applicationContext.getBean("userService");
- User user1 = new User("user1ID", "User 1");
- User user2 = new User("user2ID", "User 2");
- System.out.println("==== getting objects from redis ====");
- System.out.println("User is not in redis yet: " + userService.get(user1));
- System.out.println("User is not in redis yet: " + userService.get(user2));
- System.out.println("==== putting objects into redis ====");
- userService.put(user1);
- userService.put(user2);
- System.out.println("==== getting objects from redis ====");
- System.out.println("User should be in redis yet: " + userService.get(user1));
- System.out.println("User should be in redis yet: " + userService.get(user2));
- System.out.println("==== deleting objects from redis ====");
- userService.delete(user1);
- userService.delete(user2);
- System.out.println("==== getting objects from redis ====");
- System.out.println("User is not in redis yet: " + userService.get(user1));
- System.out.println("User is not in redis yet: " + userService.get(user2));
- }
- }
確定redis伺服器是開啟狀态之後就可以運作程式。運作結果如下: