First, the use of the cache
The access operation is consistent with the integrated Spring, only the expiration time of the key is added.
1. Create a Cachedmanager package access Method
Java code 650) this.width=650; "src=" Http://tablemiao.iteye.com/images/icon_star.png "alt=" collection Code "style=" border:0px; "/ >
@Repository
Public class Cachedmanager extends basedao{
Public boolean setcached (String key,object value,long expire) {
Try {
Super. Redistemplate.opsforvalue (). Set (key, value, expire, timeunit.minutes);
return true;
}catch(Exception e) {
Logger.info ("Add cache failed: {}", E.getmessage ());
return false;
}
}
Public Object getcached (String key) {
Try {
return Super. Redistemplate.opsforvalue (). get (key);
}catch(Exception e) {
Logger.info ("Get cache failed: {}", E.getmessage ());
return false;
}
}
Add cache time Two more parameters one is the cache time, one is the unit (nanosecond to day optional, source can be seen)
2. Test Cache a key for one minute
Java code 650) this.width=650; "src=" Http://tablemiao.iteye.com/images/icon_star.png "alt=" collection Code "style=" border:0px; "/ >
Cached.setcached ("Cached_test", "Hello----", long.valueof (1));
for (int i = 0; i < ; i++) {
Logger.info ("Get Cached data: {}, Current time: {}", cached.getcached ("Cached_test"),
New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss")
. Format (new Date ()));
Thread.CurrentThread (). Sleep (N);
}
650) this.width=650; "Src=" http://dl2.iteye.com/upload/attachment/0114/0290/ 30ffba7a-8278-3a8f-8357-cebbed897dc6.png "style=" border:0px; "/>
the results are visible and expire after one minute. See Cc-redis-tow package:cached and test classes
second, with Memcached Comparison
1. Basic Parameters
memcached 30 Day (there is a saying is permanent, but in the 30 Tin can be saved is not advisable) a key stored in value more than 1m can be split into multiple key< Span style= "font-family: ' The song Body '; > action
Redis default port 6379 Cache default expiration time does not expire (if the default persistence mode is canceled, data is not persisted to disk, and when memory is exceeded,Redis uses the LRU mechanism to use the least recently used key clears, and writes new data) a key that stores a value of up to 1g bytes of data
2. Storage Type
Memcached is similar to a huge hashTable
Redis Storage Type diverse, bottom-saving byte array
3. Persistence of data
memcached save data in memory, after exceeding, clear invalid key,LRU mechanism to clear the least recently used key
Redis Storage Data can be persisted to disk in memory and can continue to load using after reboot
650) this.width=650; "Width=" "class=" Zoom "id=" Aimg_o1yt0 "src=" http://gd2.alicdn.com/imgextra/i2/332189337/ Tb2q1occvxxxxchxpxxxxxxxxxx-332189337.png "border=" 0 "/>
get ""
Redis cache usage, differences with memcached