Package Com.fndsoft.bcis.utils;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.data.redis.core.*; import Org.springframework.stereotype.service;import java.util.*;/** * Redis Cache helper class * Created by DELL on 2016/5/23.*/@Service Public classRediscacheutil<t>{@Autowired Publicredistemplate redistemplate; /** * Cache Basic objects, Integer, String, entity class, etc. * * @param key cache keys * @param value Cached values * @return Cached objects */ Public<T> valueoperations<string, t>Setcacheobject (String key, T value) {valueoperations<string, t> operation =Redistemplate.opsforvalue (); Operation.Set(key, value); returnoperation; } /** * Gets the base object of the cache. * * @param key Cache key value * @return The data corresponding to the cache key value*/ Public<T>T getcacheobject (String key) {valueoperations<string, t> operation =Redistemplate.opsforvalue (); returnOperation.Get(key); } /** * Cache list Data * * @param key cache keys * @param dataList list data to be cached * @return Cached objects*/ Public<T> listoperations<string, t> setcachelist (String key, list<t>dataList) {listoperations listoperation=redistemplate.opsforlist (); if(NULL!=dataList) { intSize =datalist.size (); for(inti =0; i < size; i++) {Listoperation.leftpush (key, DataList.Get(i)); } } returnlistoperation; } /** * Gets the cached list Object * * @param key value of the key cache * @return the cache key value corresponding to the data*/ Public<T> list<t>getcachelist (String key) {List<T> dataList =NewArraylist<t>(); Listoperations<string, t> listoperation =redistemplate.opsforlist (); Long size=listoperation.size (key); for(inti =0; i < size; i++) {Datalist.add (Listoperation.index (key,i)); } returndataList; } /** * Cache Set * * @param key Cache Key value * @param DataSet Cached Data * @return object that caches data*/ Public<T> boundsetoperations<string, t> setcacheset (String key, set<t>DataSet) {boundsetoperations<string, t> setoperation =Redistemplate.boundsetops (key); Iterator<T> it =Dataset.iterator (); while(It.hasnext ()) {Setoperation.add (It.next ()); } returnsetoperation; } /** * Get cached set * * @param key * @return*/ PublicSet<t>Getcacheset (String key) {Set<T> DataSet =NewHashset<t>(); Boundsetoperations<string, t> operation =Redistemplate.boundsetops (key); Long size=operation.size (); for(inti =0; i < size; i++) {Dataset.add (Operation.pop ()); } returnDataSet; } /** * Cache map * * @param key * @param dataMap * @return*/ Public<T> hashoperations<string, String, t> setcachemap (string key, Map<string, t>DataMap) {hashoperations hashoperations=Redistemplate.opsforhash (); if(NULL!=DataMap) { for(Map.entry<string, t>Entry:dataMap.entrySet ()) {Hashoperations.put (Key, Entry.getkey (), Entry.getvalue ()); } } returnhashoperations; } /** * Get Cached Map * * @param key * @return*/ Public<T> map<string, t>Getcachemap (String key) {Map<string, t> map =Redistemplate.opsforhash (). Entries (key); returnmap; } /** * Cache map * * @param key * @param dataMap * @return*/ Public<T> hashoperations<string, Integer, t> setcacheintegermap (String key, Map<integer, t>DataMap) {hashoperations hashoperations=Redistemplate.opsforhash (); if(NULL!=DataMap) { for(Map.entry<integer, t>Entry:dataMap.entrySet ()) {Hashoperations.put (Key, Entry.getkey (), Entry.getvalue ()); } } returnhashoperations; } /** * Get Cached Map * * @param key * @return*/ Public<T> Map<integer, t>Getcacheintegermap (String key) {Map<integer, t> map =Redistemplate.opsforhash (). Entries (key); returnmap; }}
The following is a test method for storing a list object:
/** * Redis Cache List Object*/@Test Public voidsetcatchvalueforlist () {String key="User_test7"; List<Code> codeList =NewArraylist<>(); Code Code1=NewCode ("436436",533); Code Code2=NewCode ("214214",53453); Codelist.add (CODE1); Codelist.add (CODE2); Rediscacheutil.setcachelist (key, codeList); System. out. println ("Save data Successfully! "); } @Test Public voidgetvalueforlist () {String key="User_test7"; List<Code> codeList =rediscacheutil.getcachelist (key); for(Code code:codelist) {System. out. println (Code.getname () +" "+code.getage ()); } }
Redis several data types to data inventory data and help classes to fetch data