Pom.xml Introducing Redis Open Cache
<!-- Cache--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis-
<dependency>
<groupId>org.springframework.boot</groupId>
< Artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>
application.properties configuration file
# Redis Database index (default = 0)
spring.redis.database=0
# Redis server address
spring.redis.host=localhost
# Redis Server connection Port
spring.redis.port=6379
# Redis Server connection password (default is empty)
spring.redis.password=
# Connection pool Maximum number of connections (using negative values for No limit)
spring.redis.pool.max-active=8
# Connection pool maximum blocking wait time (using negative values to indicate no limit)
Spring.redis.pool.max-wait=-1
# Maximum idle connection in the connection pool
spring.redis.pool.max-idle=8
# Minimum idle
connection in the connection pool Spring.redis.pool.min-idle=0
# Connection time-out (ms)
spring.redis.timeout=0
Redisservice
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.redis.core.*;
Import Org.springframework.stereotype.Service;
Import java.io.Serializable;
Import java.util.List;
Import Java.util.Set;
Import Java.util.concurrent.TimeUnit;
/** * Created by Administrator on 2017/12/1.
*/@Service public class Redisservice {@Autowired private redistemplate redistemplate; /** * Write Cache * @param key * @param value * @return */public Boolean set (Final String key, Obje
CT value) {Boolean result = false;
try {valueoperations<serializable, object> operations = Redistemplate.opsforvalue ();
Operations.set (key, value);
result = true;
} catch (Exception e) {e.printstacktrace ();
} return result; }/** * Write Cache Settings Aging time * @param key * @param value * @return */public Boolean set (final Stri Ng Key, ObjecT value, Long expiretime) {Boolean result = false;
try {valueoperations<serializable, object> operations = Redistemplate.opsforvalue ();
Operations.set (key, value);
Redistemplate.expire (Key, Expiretime, timeunit.seconds);
result = true;
} catch (Exception e) {e.printstacktrace ();
} return result; }/** * Bulk delete the corresponding value * @param keys */public void Remove (final String ... keys) {for (Strin
G Key:keys) {remove (key);
}}/** * Bulk Delete key * @param pattern */public void Removepattern (final String pattern) {
set<serializable> keys = Redistemplate.keys (pattern);
if (keys.size () > 0) redistemplate.delete (keys); }/** * Delete the corresponding value * @param key */public void Remove (final String key) {if (Exists (key)) {Redistemplate.delete (key); }}/** * To determine if there is a corresponding value in the cache * @param key * @return */public Boolean exists (final String ke
Y) {return redistemplate.haskey (key); }/** * Read cache * @param key * @return * * * public Object get (final String key) {object re
Sult = null;
Valueoperations<serializable, object> operations = Redistemplate.opsforvalue ();
result = Operations.get (key);
return result; }/** * Hash add * @param key * @param hashkey * @param value */public void Hmset (String ke
Y, Object HashKey, Object value) {hashoperations<string, object, object> hash = Redistemplate.opsforhash ();
Hash.put (Key,hashkey,value); /** * Hash gets data * @param key * @param hashkey * @return */public Object hmget (String key
, Object HashKey) {hashoperations<string, object, object> hash = Redistemplate.opsforhash (); Return Hash.get (Key,hashkey); }/** * List Add * @param k * @param v */public void Lpush (String k,object v) {Listopera
tions<string, object> list = Redistemplate.opsforlist ();
List.rightpush (K,V); }/** * @param k * @param l * @param L1 * @return */Public LIST<OBJECT&G T
Lrange (String K, long L, long L1) {listoperations<string, object> list = Redistemplate.opsforlist ();
Return List.range (K,L,L1);
}/** * Collection Add * @param key * @param value */public void Add (String key,object value) {
Setoperations<string, object> set = Redistemplate.opsforset ();
Set.add (Key,value);
}/** * Collection gets * @param key * @return */public set<object> setmembers (String key) {
Setoperations<string, object> set = Redistemplate.opsforset ();
return Set.members (key);
}
/** * Ordered collection Add * @param key * @param value * @param scoure */public void Zadd (String key,object val
Ue,double scoure) {zsetoperations<string, object> zset = Redistemplate.opsforzset ();
Zset.add (key,value,scoure); }/** * @param key * @param scoure * @param scoure1 * @return */Public Se T<object> Rangebyscore (String key,double scoure,double scoure1) {zsetoperations<string, object> zset =
Redistemplate.opsforzset ();
Return Zset.rangebyscore (Key, Scoure, Scoure1); }
}
controller used for testing
Import Com.example.service.RedisService;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RestController;
@RestController public
class Democontroller {
@Autowired
private redisservice redisservice;
@RequestMapping (value = "/test", method = requestmethod.post) public
void Demotest () {
redisservice.set ("1", "value22222");
}
}
successful use of postman Access test
using the REDIS-CLI client to view
The code was copied online, and the test was done.