1. 程式人生 > >對於Memcache和Java hashMap比較

對於Memcache和Java hashMap比較

因隊友陷落,還是自問自答吧…

二者相同點

二者都是key-value

且都是通過hash來儲存

都是記憶體儲存

二者不同

Advantages of Java memory over memcache:

  • Java memory is faster (no network).
  • Java memory won’t require serialization, you have Java objects available to you.

Advantages of memcache over Java memory

  • It can be accessed by more than one application server, so your cache will be shared among all your app servers.
  • It can be accessed by a variety of different servers, so long as they all agree on the key scheme and the serialization.
  • It will discard expired cache values, so you get time-based invalidation.

    a benchmark between a concurrent hash map, memcached, and MySQL.

a benchmark between a concurrent hash map, memcached, and MySQL.

memcached儲存機制

訪問方式

為了在記憶體中提供資料的高速查詢能力,memcache使用 key-value 形式儲存和訪問資料,在記憶體中維護一張巨大的 HashTable,使得對資料查詢的時間複雜度降低到 O(1 ),保證了對資料的高效能訪問。

記憶體釋放演算法

記憶體的空間總是有限的,當記憶體沒有更多的空間來儲存新的資料時,memcache就會使用 LRU (Least Recently Used)演算法,將最近不常訪問的資料淘汰掉,以騰出空間來存放新的資料。

儲存支援的資料格式

memcache儲存支援的資料格式也是靈活多樣的,通過物件的序列化機制,可以將更高層抽象的物件轉換成為二進位制資料,儲存在快取伺服器中,當前端應用需要時,又可以通過二進位制內容反序列化,將資料還原成原有物件。

附錄

https://code.google.com/p/kitty-cache/
Java 記憶體快取
超級簡單的區域性使用的高效能執行緒安全設計java.util.concurrent包記憶體快取。 這是隻有75行程式碼,包括註釋和getter。

來源