Redis cache usage, differences with memcached

Source: Internet
Author: User

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
  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
  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 (5000);
  7. }

The results are visible and expire after one minute. See Cc-redis-tow package:cached and test classes

Ii. comparison with memcached

1. Basic parameters

memcached default port 11211 cache default Expiration Time 30 days (one term is permanent, but can not be saved after 30 days) the value stored in a key is up to 1M, and more than 1M can be split into multiple keys for operation

Redis default port 6379 cache default expiration time does not expire (if default persistence is canceled, data is not persisted to disk, Redis uses the LRU mechanism to erase the most recently used key when memory is exceeded, and writes new data) the value stored in a key is the most 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


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.