Welcome to join Java Kochi Group communication
650) this.width=650; "Src=" http://www.91display.com/javagaozhi/images/8f8672fc-1293-48e5-b1d3-e09c0a1e2db3/ Qrcode.jpg "height="/>
Springboot Integrated Redis is simple
1. Introducing MAVEN-dependent Redis packages
<!--springboot integrated Redis--><dependency> <groupId>org.springframework.boot</groupId> <art Ifactid>spring-boot-starter-redis</artifactid></dependency>
2. Configuring the Redis environment in the APPLICATION.YML configuration file
Spring:redis:host:localhost #password: Redispassword port:6379 pool:max-idle:100 min-idle:1 max-active:1000 max-wait:-1
3. Writing configuration file Redisconfig
package com.kinder.redis;import java.lang.reflect.method;import org.springframework.cache.cachemanager;import org.springframework.cache.annotation.cachingconfigurersupport;import org.springframework.cache.annotation.enablecaching;import org.springframework.cache.interceptor.keygenerator;import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration;import org.springframework.data.redis.cache.rediscachemanager;import org.springframework.data.redis.connection.redisconnectionfactory;import org.springframework.data.redis.core.redistemplate;import org.springframework.data.redis.core.stringredistemplate;import org.springframework.data.redis.serializer.jackson2jsonredisserializer;import com.fasterxml.jackson.annotation.jsonautodetect;import com.fasterxml.jackson.annotation.propertyaccessor; import com.fasterxml.jackson.databind.objectmapper; @Configuration @enablecachingpublic class redisconfig extends cachingconfigurersupport {/** * strategy for generating key * * @return */@Beanpublic keygenerator keygenerator () {return New keygenerator () {@Overridepublic object generate (object target, method Method, object... params) {stringbuilder sb = new stringbuilder (); sb.append (Target.getclass (). GetName ()); Sb.append (Method.getname ());for (object obj : params) { Sb.append (Obj.tostring ());} Return sb.tostring ();}};} /** * managing Cache */@Beanpublic cachemanager cachemanager (redistemplate redistemplate) {rediscachemanager rcm = new rediscachemanager (redistemplate); RETURN&NBSP;RCM;} /** * redistemplate Configuration */@Beanpublic RedisTemplate<String, String> Redistemplate (redisconnectionfactory factory) {stringredistemplate&nbsP;template = new stringredistemplate (Factory); Jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer ( Object.class); Objectmapper om = new objectmapper (); Om.setvisibility (PropertyAccessor.ALL, jsonautodetect.visibility.any); om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL); Jackson2jsonredisserializer.setobjectmapper (OM); Template.setvalueserializer (Jackson2jsonredisserializer); Template.afterpropertiesset (); return template;}}
4. Service class to write Redis services
package com.kinder.redis;import java.io.serializable;import java.util.list;import java.util.set;import java.util.concurrent.timeunit;import org.springframework.beans.factory.annotation.autowired;import org.springframework.data.redis.core.hashoperations;import org.springframework.data.redis.core.listoperations;import org.springframework.data.redis.core.redistemplate;import org.springframework.data.redis.core.setoperations;import org.springframework.data.redis.core.valueoperations;import org.springframework.data.redis.core.zsetoperations;import org.springframework.stereotype.service;@ servicepublic class redisservice {@Autowiredprivate redistemplate redistemplate;/** * 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 Settings aging time * * @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;} /** * Bulk Delete the corresponding value * * @param keys */public void remove ( Final string... keys) {for (String 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);}} /** * determine if there is a corresponding value * * @param key * @return in the cache */public boolean exists (Final string key) {return redistemplate.haskey (key);} /** * Read Cache * * @param key * @return */public object get (Final string key) {valueoperations<serializable, object> operations = redistemplate.opsForvalue (); Return operations.get (key);} /** * Hash Add * * @param key * @param hashkey * @param value */public void hmset (string key, object hashkey, Object value) {HashOperations<String, Object, Object> hash = Redistemplate.opsforhash (); Hash.put (Key, hashkey, value);} /** * hashing Get 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&NBSP;K,&NBSP;OBJECT&NBSP;V) {ListOperations<String, Object> list = Redistemplate.opsforlist (); List.rightpush (K,&NBSP;V);} /** * list Get * * @param k * @param l * @param l1 * @return */public list<object> lrange (String k, long l, &NBSP;LONG&NBSP;L1) {ListOperations<String, Object> list = Redistemplate.opsforlist (); Return list.range (K,&NBSP;L,&NBSP;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 Get * * @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 value, double scoure) {zsetoperations<string, object> zset = redistemplate.opsforzset (); Zset.add (key , value, scoure);} /** * ordered collection acquisition * * @param key * @param scoure * @ param scoure1 * @return */public set<object> rangebyscore (String key, double scoure, double scoure1) {zsetoperations<string, object> zset = redistemplate.opsforzset (); Return zset.rangebyscore (Key, scoure, scoure1);}}
5. Operation of Redis.
package com.kinder.demo.controller;import java.util.hashmap;import java.util.map;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody;import com.kinder.redis.redisservice;import Com.kinder.util.resultutil;/** * redis Operation * * @author administrator * */@Controller @requestmapping ("/demo/redis") public class rediscontroller {@ autowiredprivate redisservice redisservice;/** * setting up Redis data * * @param key * @param value * @return */@RequestMapping ("/setredis.do") @ Responsebodypublic object setredis (String key, string value) {redisService.set ( Key, value); return resultutil.returnsuccess ();} /** *&nTo delete redis data * * @param key * @return */@RequestMapping ("/ Delredis.do ") @ResponseBodypublic object delredis (String key) {redisservice.remove (key); Return resultutil.returnsuccess ();} /** * redis data * * @param key * @return based on key */@ Requestmapping ("/getredis.do") @ResponseBodypublic object getredis (String key) {object value = redisservice.get (key); Map<string, object> map = new hashmap<string, object> (); Map.put (" Value ", value); return resultutil.returnsuccess (map);}}
The end.
Welcome to join Java Kochi Group communication
650) this.width=650; "Src=" http://www.91display.com/javagaozhi/images/8f8672fc-1293-48e5-b1d3-e09c0a1e2db3/ Qrcode.jpg "height="/>
Springboot Integrated Redis Detailed