Redis cache usage, differences with memcached

Source: Internet
Author: User
Tags getmessage memcached

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; "/ >

  1. @Repository

  2. Public class Cachedmanager extends basedao{

  3. Public boolean setcached (String key,object value,long expire) {

  4. Try {

  5. Super. Redistemplate.opsforvalue (). Set (key, value, expire, timeunit.minutes);

  6. return true;

  7. }catch(Exception e) {

  8. Logger.info ("Add cache failed: {}", E.getmessage ());

  9. return false;

  10. }

  11. }

  12. Public Object getcached (String key) {

  13. Try {

  14. return Super. Redistemplate.opsforvalue (). get (key);

  15. }catch(Exception e) {

  16. Logger.info ("Get cache failed: {}", E.getmessage ());

  17. return false;

  18. }

  19. }

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; "/ >

  1. Cached.setcached ("Cached_test", "Hello----", long.valueof (1));

  2. for (int i = 0; i < ; i++) {

  3. Logger.info ("Get Cached data: {}, Current time: {}", cached.getcached ("Cached_test"),

  4. New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss")

  5. . Format (new Date ()));

  6. Thread.CurrentThread (). Sleep (N);

  7. }

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

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.