Spring Data Redis example, springredis
Description
About Redis: A NoSQL memory database stored based on key-value pairs can store complex data structures, suchList
,Set
,Hashes
.
Spring Data Redis (SDR) makes it easier for Spring applications to configure and access Redis.
This project is implemented based on spring boot, spring data redis, and spring io platform.
POM Configuration
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.edu.hdu</groupId> <artifactId>examples.sdr</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>examples.sdr</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>Athens-SR2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>examples.sdr</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.3.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>
Configure RedisTemplate Bean
Two beans are generated: JedisConnectionFactory and RedisTemplate. The RedisTemplate bean is used for subsequent injection to the PersonRepoImpl to operate Redis databases.
@Configurationpublic class RedisConfig{ @Bean JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); connectionFactory.setHostName("127.0.0.1"); connectionFactory.setPort(6379); return new JedisConnectionFactory(); } @Bean @Autowired public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); return template; } }
Use RedisTemplate to operate Redis
Here we get the RedisTemplateHashOperations
Object To access Redis data.
package cn.edu.hdu.examples.sdr.repo.impl;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Repository;import cn.edu.hdu.examples.sdr.bean.Person;import cn.edu.hdu.examples.sdr.repo.PersonRepo;@Repositorypublic class PersonRepoImpl implements PersonRepo { @Autowired private RedisTemplate<String, Object> redisTemplate; private static String PERSON_KEY = "Person"; @Override public void save(Person person) { this.redisTemplate.opsForHash().put(PERSON_KEY, person.getId(), person); } @Override public Person find(String id) { return (Person) this.redisTemplate.opsForHash().get(PERSON_KEY, id); } @Override public Map<Object, Object> findAll() { return this.redisTemplate.opsForHash().entries(PERSON_KEY); } @Override public void delete(String id) { this.redisTemplate.opsForHash().delete(PERSON_KEY, id); }}
Test
Run the following code for testing. Run the local redis service before running it;
1. view all current users
2. store three users
3. View users with ID 3
4. View All Users
5. Delete the user whose ID is 2 and view the remaining user
@ SpringBootApplicationpublic class Application implements CommandLineRunner {@ Autowired private PersonRepo personRepo; public void testHash () {Map <Object, Object> personMatrixMap = personRepo. findAll (); System. out. println ("@ All Users stored in current Redis:" + personMatrixMap); Person person = new Person (); person. setId ("1"); person. setAge (55); person. setGender (Gender. female); person. setName ("Oracle"); personRepo. save (person); Person person2 = new Person (); person2.setId ("2"); person2.setAge (60); person2.setGender (Gender. male); person2.setName ("theeffecect"); personRepo. save (person2); Person person3 = new Person (); person3.setId ("3"); person3.setAge (25); person3.setGender (Gender. male); person3.setName ("TheOne"); personRepo. save (person3); System. out. println ("Search for users with ID 3:" + personRepo. find ("3"); personMatrixMap = personRepo. findAll (); System. out. println ("all users currently stored in Redis:" + personMatrixMap); personRepo. delete ("2"); personMatrixMap = personRepo. findAll (); System. out. println ("delete all users with ID 2:" + personMatrixMap) ;}@ Override public void run (String... args) throws Exception {this. testHash ();} public static void main (String [] args) {// Close the context so it doesn't stay awake listening for redis SpringApplication. run (Application. class, args ). close ();}}
Result printing:
@ All Users stored in Redis :{}
Search for users with ID 3: Person [id = 3, name = TheOne, gender = Male, age = 25]
All users stored in Redis: {2 = Person [id = 2, name = theeffecect, gender = Male, age = 60], 3 = Person [id = 3, name = TheOne, gender = Male, age = 25], 1 = Person [id = 1, name = Oracle, gender = Female, age = 55]}
After deleting a user whose ID is 2, all remaining users: {3 = Person [id = 3, name = TheOne, gender = Male, age = 25], 1 = Person [id = 1, name = Oracle, gender = Female, age = 55]}
You can also open redis-cli and manually enter the key to view the current Redis storage result:
The remaining two users are: {3 = Person [id = 3, name = TheOne, gender = Male, age = 25], 1 = Person [id = 1, name = Oracle, gender = Female, age = 55]}
Example source code
Https://github.com/peterchenhdu/spring-data-redis-example
References
Https://examples.javacodegeeks.com/enterprise-java/spring/spring-data-redis-example/