Integrated Redis one. Annotation mode implementation Add cache 1. Join the dependency in Pom.xml
<!---<dependency> <GroupId >org.springframework.boot</groupId> < artifactid>spring-boot-starter-redis</artifactid ></dependency>
2. Modifying the Boot class
Modify open cache, add annotations @enablecaching
Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; Import org.springframework.cache.annotation.EnableCaching, @SpringBootApplication @enablecaching Public class Application { publicstaticvoid main (string[] args) { Springapplication.run (Application. class , args);} }
3. Setting the implementation of the serialization interface
To cache entities in Redis, you need to have the entity implement the serialization interface
Public class Implements Serializable { private Long ID; Private String userName; Private String password; Private String name; }
4. Implementing Add/Remove Caches
Modify Userserviceimpl,
Adding @cacheable annotations to implement cache additions
Adding @cacheevict annotations for cache deletion
@Override @cacheevict (value= "Usercache", key = "' User.queryall '") PublicList<user>queryuserbyname (String name) {System.out.println ("Cache cleaned up!" "); List<User> list = This. Usermapper.queryuserbyname (name); returnlist;}//Call the mapper using Usermapper.xml@Override @cacheable (value= "Usercache", key = "' User.queryall '") PublicList<user>Queryall () {System.out.println ("Query from MySQL"); List<User> list = This. Usermapper.queryall (); returnlist;}
When this is done, the Execute Queryall () method uses the cache, if the cache is not added, and the Queryuserbyname (String name) method deletes the cache
@Cacheable: Add/Use Cache
@CacheEvict: Delete Cache
The parameter value is the name of the cache, and when executed, the cache used/deleted is called for.
Parameter key by default is an empty string "", is a spring expression language spel, we can arbitrarily specify, but need to note that must be added single quotation marks
Two. Deep use of Redis 1. Direct operation of Redis
In addition to being used as a cache, Redis has many other functions, such as using Redis's single thread to get unique numbers, such as using Redis as a single sign-on system to store user login information, and we need to operate redis directly.
The official website provides three kinds of interface redisconnectionfactory, Stringredistemplate and redistemplate, we can directly inject or implement other implementation classes, to directly operate Redis. We use Redistemplate to operate Redis here.
As shown below, we only need to inject redistemplate directly to manipulate the five different data types of Redis using the following methods:
Test:
@AutowiredPrivateRedistemplate<string, string>redistemplate; @Override @cacheevict (Value= "Usercache", key = "' User.findall '") PublicList<user>queryuserbyname (String name) {//Save Data This. Redistemplate.boundvalueops ("Redis"). Set ("Hello Redis!"); //set the effective time to 100 seconds This. Redistemplate.boundvalueops ("Redis"). Expire (100l, Timeunit.seconds); //add one action to value at a time This. Redistemplate.boundvalueops ("Count"). Increment (1l); System.out.println ("Cache cleaned up!" "); List<User> list = This. Usermapper.queryuserbyname (name); returnlist;}
2. Setting Redis Connection Properties
Redis stand-alone version
Redis Launcher By default, the local Redis service is found, the port number is 6379 by default if you need to access Redis for other servers, you need to configure the following in Application.properties:
#Redisspring. Redis.host=192.168.37.161spring.redis.port=6379
This means we're going to look for a service with IP 192.168.37.161 and Port 6379.
Redis Cluster Edition
#Redis #spring.redis.host=192.168.37.161#spring. Redis.port=6379#Redis Clusterspring.redis.cluster.nodes= 192.168.37.161:7001,192.168.37.161:7002,192.168.37.161:7003,192.168.37.161:7004,192.168.37.161:7005,192.168.37.161:7006
Switch to the cluster version only need to do the above configuration, configure the cluster node information, comment out the standalone version of the information
Viii. Springboot Integrated Redis