Springboot + Redis

Source: Internet
Author: User
Tags delete key

1. Add a Dependency package

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. Configure Redis Properties

Spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
Spring.redis.pool.max-active=8
Spring.redis.pool.max-wait=-1
Spring.redis.pool.max-idle=8
Spring.redis.pool.min-idle=0
Spring.redis.timeout=0

3. Create a Redis cache class----"Rediscacheconfig.java

@Configuration
@EnableCaching
public class Rediscacheconfig {
@Bean
public CacheManager CacheManager (redistemplate<?,? > Redistemplate) {
CacheManager CacheManager = new Rediscachemanager (redistemplate);
return CacheManager;
}
@Bean
public redistemplate<string, string> redistemplate (Redisconnectionfactory factory) {
redistemplate<string, string> redistemplate = new redistemplate<string,string> ();
Redistemplate.setconnectionfactory (Factory);
//Key serialization mode, (otherwise garbled;), but if the method has a long and other non-string type, the type conversion error will be reported;
//So the following code does not recommend this when you do not define a key generation strategy. Can not configure or implement Objectredisserializer
//or Jdkserializationredisserializer serialization mode;
redisserializer<string> Redisserializer = new Stringredisserializer ();//long type can not appear exception information;
Redistemplate.setkeyserializer (Redisserializer);
Redistemplate.sethashkeyserializer (Redisserializer);
return redistemplate;
}
}

4. Create Redis Tool class----"Redisutil.java

@SuppressWarnings ("Unchecked")
@Component
public class Redisutil {
@SuppressWarnings ("Rawtypes")
@Autowired
Private Redistemplate redistemplate;
/**
* Delete the corresponding value in bulk
*
* @param keys
*/
public void Remove (final String ... keys) {
for (String Key:keys) {
Remove (key);
}
}
/**
* Delete key in bulk
*
* @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);
}
}
/**
* Determine if there is a corresponding value in the cache
*
* @param key
* @return
*/
Public Boolean exists (final String key) {
return Redistemplate.haskey (key);
}
/**
* Read Cache
*
* @param key
* @return
*/
public string get (final string key) {
Object result = null;
Redistemplate.setvalueserializer (New Stringredisserializer ());
Valueoperations<serializable, object> operations = Redistemplate.opsforvalue ();
result = Operations.get (key);
if (result==null) {
return null;
}
return result.tostring ();
}
/**
* Write Cache
*
* @param key
* @param value
* @return
*/
Public Boolean set (Final String key, Object 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
*
* @param key
* @param value
* @return
*/
Public Boolean set (Final String 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;
}

public boolean Hmset (String key, map<string, string> value) {
Boolean result = false;
try {
Redistemplate.opsforhash (). Putall (key, value);
result = true;
} catch (Exception e) {
E.printstacktrace ();
}
return result;
}

Public map<string,string> Hmget (String key) {
map<string,string> result =null;
try {
result= Redistemplate.opsforhash (). Entries (key);
} catch (Exception e) {
E.printstacktrace ();
}
return result;
}
}

5. Write the test code (to count the number of users as an example, write the number of users to the cache, and then read)

1. Get the number of users through timed tasks and write to cache

@Scheduled (cron = "*/10 * * * * *?")
public void Runjoba () {
Long UserCount = 0l;
list<sysuser> list = sysuserservice.getuserlist (null, NULL, NULL);
if (list! = null) {
UserCount = (long) list.size ();
}
System.out.println (UserCount);
Redisutil.set ("UserCount", usercount.tostring (), 100l);
}

2. Get the number of users from the cache

Add the following code in the Sysusercontroller.java

@RequestMapping (value= "/getusercount")

@ResponseBody
Public String Getusercount () {
return Sysuserservice.getusercount ();
}

Add the following code in Sysuserservice.java

Public String Getusercount () {
Return Redisutil.get ("UserCount");
}

6. Testing

Springboot + Redis

Related Article

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.