Redis是一种nosql 数据库,在开发中常用做缓存。Jedis是Redis在java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用。建立Maven Project之后,在POM.xml中添加jedis和spring-data-redis的依赖如下:
[html] view plaincopy
1. <dependency>
2. <groupId>redis.clients</groupId>
3. <artifactId>jedis</artifactId>
4. <version>2.0.0</version>
5. <type>jar</type>
6. <scope>compile</scope>
7. </dependency>
8. <!-- spring-redis -->
9. <dependency>
10. <groupId>org.springframework.data</groupId>
11. <artifactId>spring-data-redis</artifactId>
12. <version>1.0.0.RELEASE</version>
13. </dependency>
Redis连接数据库参数如下:applicationContext-redis.properties
[plain] view plaincopy
1. #redis config
2. redis.pool.maxActive=100
3. redis.pool.maxIdle=20
4. redis.pool.maxWait=1000
5. redis.pool.testOnBorrow=true
6. redis.hostname=localhost
7. redis.port=6379
8. redis.password=
在上下文配置中使用key-value读取方式读取properties中的值:
[html] view plaincopy
1. <!-- Jedis 连接池配置-->
2. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
3. <property name="maxActive" value="${redis.pool.maxActive}" />
4. <property name="maxIdle" value="${redis.pool.maxIdle}" />
5. <property name="maxWait" value="${redis.pool.maxWait}" />
6. <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
7. </bean>
8. <!-- Jedis ConnectionFactory 数据库连接配置-->
9. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
10. <property name="hostName" value="${redis.hostname}" />
11. <property name="port" value="${redis.port}" />
12. <property name="password" value="${redis.password}" />
13. <property name="poolConfig" ref="jedisPoolConfig" />
14. </bean>
15. <!—- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
16. <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 plaincopy
1. public class User implements Serializable {
2.
3. private static final long serialVersionUID = -7898194272883238670L;
4.
5. public static final String OBJECT_KEY = "USER";
6.
7. public User() {
8. }
9.
10. public User(String id) {
11. }
12.
13. public User(String id, String name) {
14. this.id = id;
15. this.name = name;
16. }
17.
18. private String id;
19.
20. private String name;
21.
22. public String getId() {
23. return id;
24. }
25.
26. public void setId(String id) {
27. this.id = id;
28. }
29.
30. public String getName() {
31. return name;
32. }
33.
34. public void setName(String name) {
35. this.name = name;
36. }
37.
38. public String toString() {
39. return "User [id=" + id + ", name=" + name + "]";
40. }
41.
42. public String getKey() {
43. return getId();
44. }
45.
46. public String getObjectKey() {
47. return OBJECT_KEY;
48. }
49. }
创建userService类来操作redis增删查改缓存对象。
[java] view plaincopy
1. public class UserService {
2.
3. RedisTemplate<String, User> redisTemplate;
4.
5. public RedisTemplate<String, User> getRedisTemplate() {
6. return redisTemplate;
7. }
8.
9. public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
10. this.redisTemplate = redisTemplate;
11. }
12.
13. public void put(User user) {
14. redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
15. }
16.
17. public void delete(User key) {
18. redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey());
19. }
20.
21. public User get(User key) {
22. return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey());
23. }
24. }
在上下文中配置redisTemplate注入,在使用bean方式来注入时,redisTemplate必须有setter/getter方法:
[html] view plaincopy
1. <bean id="userService" class="Service.UserService">
2. <property name="redisTemplate">
3. <ref bean="redisTemplate" />
4. </property>
5. </bean>
======如果使用注解方式自动注入,则可以注释掉上面的bean配置方式======
在UserService注解@Service(“userService”),也可以在Service里写名字,默认是第一字母小写。
[java] view plaincopy
1. @Service ("userService")
2. public class UserService {
3.
4. @Autowired
5. RedisTemplate<String, User> redisTemplate;
6. ……
7. ……
8. }
在上下文配置文件中,添加自动扫描包的context节点,Base-package的路径要覆盖包含注解的类文件:
[html] view plaincopy
- <context:component-scan base-package="*" />
在main中来简单操作一下:
[java] view plaincopy
1. public class Main {
2. public static void main( String[] args )
3. {
4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/conf/applicationContext.xml" );
5. UserService userService = (UserService) applicationContext.getBean("userService");
6.
7. User user1 = new User("user1ID", "User 1");
8. User user2 = new User("user2ID", "User 2");
9.
10. System.out.println("==== getting objects from redis ====");
11. System.out.println("User is not in redis yet: " + userService.get(user1));
12. System.out.println("User is not in redis yet: " + userService.get(user2));
13.
14. System.out.println("==== putting objects into redis ====");
15. userService.put(user1);
16. userService.put(user2);
17.
18. System.out.println("==== getting objects from redis ====");
19. System.out.println("User should be in redis yet: " + userService.get(user1));
20. System.out.println("User should be in redis yet: " + userService.get(user2));
21.
22. System.out.println("==== deleting objects from redis ====");
23. userService.delete(user1);
24. userService.delete(user2);
25.
26. System.out.println("==== getting objects from redis ====");
27. System.out.println("User is not in redis yet: " + userService.get(user1));
28. System.out.println("User is not in redis yet: " + userService.get(user2));
29.
30. }
31. }
确保redis服务器是开启状态之后就可以运行程序。运行结果如下: