Java implements LRU (least recently used) cache

Source: Internet
Author: User

  1. Package com.jd.test;
  2. Import java.io.Serializable;
  3. Import Java.util.LinkedHashMap;
  4. Import Java.util.concurrent.locks.Lock;
  5. Import Java.util.concurrent.locks.ReentrantLock;
  6. /**
  7. * Cache Class (least recently unused)
  8. *
  9. * @author Liuzhenfeng
  10. *
  11. * @param <K,V>
  12. */
  13. Public class Lrucache<k, v> extends Linkedhashmap<k, v> implements Serializable {
  14. /** 
  15. * Cache Default Size
  16. */
  17. public static final int default_capasity = 20;
  18. /** 
  19. * Cache Actual Size
  20. */
  21. public static int cache_capasity = default_capasity;
  22. /** 
  23. * Thread Sync Lock
  24. */
  25. private static final lock lock = new Reentrantlock ();
  26. Public LRUCache () {
  27. super (default_capasity);
  28. cache_capasity = default_capasity;
  29. }
  30. Public LRUCache (int size) {
  31. super (size);
  32. cache_capasity = size;
  33. }
  34. /* 
  35. * Clear Cache
  36. *
  37. * @see Java.util.linkedhashmap#clear ()
  38. */
  39. @Override
  40. public void Clear () {
  41. try {
  42. Lock.lock ();
  43. super.clear ();
  44. } finally {
  45. Lock.unlock ();
  46. }
  47. }
  48. /* 
  49. * Determine if the object is included
  50. *
  51. * @see Java.util.linkedhashmap#containsvalue (java.lang.Object)
  52. */
  53. @Override
  54. Public Boolean containsvalue (Object value) {
  55. try {
  56. Lock.lock ();
  57. return super.containsvalue (value);
  58. } finally {
  59. Lock.unlock ();
  60. }
  61. }
  62. /* 
  63. * Querying objects from the cache
  64. *
  65. * @see Java.util.linkedhashmap#get (java.lang.Object)
  66. */
  67. @Override
  68. Public V Get (Object key) {
  69. try {
  70. Lock.lock ();
  71. return super.get (key);
  72. } finally {
  73. Lock.unlock ();
  74. }
  75. }
  76. /* 
  77. * Whether to delete the oldest unused cache object
  78. *
  79. * @see Java.util.linkedhashmap#removeeldestentry (java.util.Map.Entry)
  80. */
  81. @Override
  82. protected Boolean removeeldestentry (java.util.map.entry<k, v> eldest) {
  83. try {
  84. Lock.lock ();
  85. return this.size () > cache_capasity;
  86. } finally {
  87. Lock.unlock ();
  88. }
  89. }
  90. /* 
  91. * Determine if the key is included in the cache
  92. *
  93. * @see Java.util.hashmap#containskey (java.lang.Object)
  94. */
  95. @Override
  96. Public Boolean containskey (Object key) {
  97. try {
  98. Lock.lock ();
  99. return Super.containskey (key);
  100. } finally {
  101. Lock.unlock ();
  102. }
  103. }
  104. /* 
  105. * Determine if the cache is empty
  106. *
  107. * @see Java.util.hashmap#isempty ()
  108. */
  109. @Override
  110. Public Boolean isEmpty () {
  111. try {
  112. Lock.lock ();
  113. return super.isempty ();
  114. } finally {
  115. Lock.unlock ();
  116. }
  117. }
  118. /* 
  119. * Put in cache
  120. *
  121. * @see java.util.hashmap#put (java.lang.Object, Java.lang.Object)
  122. */
  123. @Override
  124. Public V put (K key, V value) {
  125. try {
  126. Lock.lock ();
  127. return super.put (key, value);
  128. } finally {
  129. Lock.unlock ();
  130. }
  131. }
  132. /* 
  133. * Removed from cache
  134. *
  135. * @see Java.util.hashmap#remove (java.lang.Object)
  136. */
  137. @Override
  138. Public V Remove (Object key) {
  139. try {
  140. Lock.lock ();
  141. return super.remove (key);
  142. } finally {
  143. Lock.unlock ();
  144. }
  145. }
  146. /* 
  147. * Cache Size
  148. *
  149. * @see Java.util.hashmap#size ()
  150. */
  151. @Override
  152. public int size () {
  153. try {
  154. Lock.lock ();
  155. return super.size ();
  156. } finally {
  157. Lock.unlock ();
  158. }
  159. }
  160. }

The re-entry lock (Reentrantlock) is a recursive, non-blocking synchronization mechanism. It was previously thought to be a simple alternative to synchronized, and the implementation mechanism was not too far apart. But the recent practice has found a difference between them.

Here is the official note: a reentrant mutex lock lock, which has the same basic behavior and semantics as the implicit monitor lock accessed using the Synchronized method and the statement, but is more powerful. Reentrantlock will be owned by a thread that has recently successfully acquired a lock and has not released the lock. When the lock is not owned by another thread, the thread that called lock succeeds in acquiring the lock and returns. If the current thread already owns the lock, this method will return immediately. You can use the Isheldbycurrentthread () and Getholdcount () methods to check whether this condition occurs.

It provides the lock () method:
If the lock is not persisted by another thread, gets the lock and returns immediately, setting the locked hold count to 1.
If the current thread has already held the lock, the count will be added to 1, and the method returns immediately.
If the lock is persisted by another thread, the current thread is disabled for the purpose of thread scheduling, and the thread will remain dormant until the lock is acquired, at which time the lock hold count is set to 1.

Java implements LRU (least recently used) cache

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.