Spring-Data-Redis storage object (redisTemplate) in Maven)

Source: Internet
Author: User
Tags redis server

Redis is a nosql database that is often used for caching during development. Jedis is the Redis-client of redis in java. Before that, I want to know the basic usage of redis and the use of Maven. After creating a Maven Project, add the dependency between jedis and spring-data-redis in POM. xml as follows:

 
  
   redis.clients
  jedis
  
   2.0.0
  
  
   jar
  
  
   compile
  
 
 
 
  
   org.springframework.data
  spring-data-redis
  
   1.0.0.RELEASE
  
 

Redis Connection database parameters are as follows: applicationContext-redis.properties

#redis configredis.pool.maxActive=100redis.pool.maxIdle=20redis.pool.maxWait=1000redis.pool.testOnBorrow=trueredis.hostname=localhostredis.port=6379redis.password=

In the context configuration, use the key-value reading method to read the values in properties:

 
 
  
  
  
  
 
 
 
  
  
  
  
 
 
 

The above redisTemplate has been basically configured.

Next, create a User class, which must implement or indirectly implement the Serializable interface:

Redis uses serialization to store objects. spring-data-redis has embedded the serialization function. We don't need to worry about it. We only need to call the api to use it. The SerialVersionUID field is useful for serialization expansion. In order to expand or reduce the field in the future, it will not cause deserialization errors.

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;}}

Create a userService class to perform operations on redis to add, query, modify, and cache objects.

public class UserService {RedisTemplate
 
   redisTemplate;public RedisTemplate
  
    getRedisTemplate() {return redisTemplate;}public void setRedisTemplate(RedisTemplate
   
     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());}}
   
  
 

Configure redisTemplate injection in the context. When using bean injection, the redisTemplate must have the setter/getter method:

 
  
   
  
 
====== If the annotation method is used for automatic injection, you can comment out the bean configuration method that is missing ======

In the UserService annotation @ Service ("userService"), you can also write a name in the Service. The default value is the first letter in lowercase.

@Service("userService")public class UserService {@AutowiredRedisTemplate
 
   redisTemplate;……        ……}
 

In the context configuration file, add the context node of the automatic scan package. The Base-package path must overwrite the class file containing the annotation:
 

In main, perform the following operations:

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));}}

Make sure that the redis server can run the program after it is enabled. The running result is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.