• JAVA 简单缓存实现-nacos


    定义缓存接口:

    1. public interface Cache {
    2. /**
    3. * Cache a pair of key value. If the key value already exists, the value will be overwritten.
    4. * @param key cache key
    5. * @param val cache value
    6. */
    7. void put(K key, V val);
    8. /**
    9. * Take the corresponding value from the cache according to the cache key.
    10. * @param key cache key
    11. * @return cache value
    12. */
    13. V get(K key);
    14. /**
    15. * Get the value in the cache according to the primary key, and put it into the cache after processing by the function.
    16. * @param key cache key
    17. * @param call a function, the return value of the function will be updated to the cache
    18. * @return cache value
    19. * @throws Exception callable function interface throw exception
    20. */
    21. V get(K key, Callable call) throws Exception;
    22. /**
    23. * Take the corresponding value from the cache according to the cache key, and remove this record from the cache.
    24. * @param key cache key
    25. * @return cache value
    26. */
    27. V remove(K key);
    28. /**
    29. * Clear the entire cache.
    30. */
    31. void clear();
    32. /**
    33. * Returns the number of key-value pairs in the cache.
    34. * @return number of key-value pairs
    35. */
    36. int getSize();
    37. }

    基于Java-Map的本地缓存实现:

    1. public class SimpleCache implements Cache {
    2. private Map cache;
    3. public SimpleCache(int size) {
    4. cache = new HashMap<>(size);
    5. }
    6. @Override
    7. public void put(K key, V val) {
    8. cache.put(key, val);
    9. }
    10. @Override
    11. public V get(K key) {
    12. return cache.get(key);
    13. }
    14. @Override
    15. public V get(K key, Callable call) throws Exception {
    16. if (cache.containsKey(key)) {
    17. return cache.get(key);
    18. } else {
    19. V v2 = call.call();
    20. cache.put(key, v2);
    21. return v2;
    22. }
    23. }
    24. @Override
    25. public V remove(K key) {
    26. return cache.remove(key);
    27. }
    28. @Override
    29. public void clear() {
    30. cache.clear();
    31. }
    32. @Override
    33. public int getSize() {
    34. return cache.size();
    35. }
    36. }

    基于LinkedHashMap的LruCache实现:

    1. public class LruCache implements Cache {
    2. private final Cache delegate;
    3. private Map keyMap;
    4. private K eldestKey;
    5. public LruCache(Cache delegate, int size) {
    6. this.delegate = delegate;
    7. setSize(size);
    8. }
    9. @Override
    10. public int getSize() {
    11. return delegate.getSize();
    12. }
    13. public void setSize(final int size) {
    14. keyMap = new LinkedHashMap(size, .75F, true) {
    15. private static final long serialVersionUID = 4267176411845948333L;
    16. @Override
    17. protected boolean removeEldestEntry(Map.Entry eldest) {
    18. boolean tooBig = size() > size;
    19. if (tooBig) {
    20. eldestKey = eldest.getKey();
    21. }
    22. return tooBig;
    23. }
    24. };
    25. }
    26. @Override
    27. public void put(K key, V val) {
    28. delegate.put(key, val);
    29. cycleKeyList(key);
    30. }
    31. @Override
    32. public V get(K key) {
    33. keyMap.get(key);
    34. return delegate.get(key);
    35. }
    36. @Override
    37. public V get(K key, Callable call) throws Exception {
    38. return this.delegate.get(key, call);
    39. }
    40. @Override
    41. public V remove(K key) {
    42. return delegate.remove(key);
    43. }
    44. @Override
    45. public void clear() {
    46. delegate.clear();
    47. keyMap.clear();
    48. }
    49. private void cycleKeyList(K key) {
    50. keyMap.put(key, null);
    51. if (eldestKey != null) {
    52. delegate.remove(eldestKey);
    53. eldestKey = null;
    54. }
    55. }
    56. }

    自动过期缓存实现:

    1. public class AutoExpireCache implements Cache {
    2. private long expireNanos;
    3. private Cache delegate;
    4. private HashMap keyProp = new HashMap<>();
    5. public AutoExpireCache(Cache delegate, long expireNanos) {
    6. this.expireNanos = expireNanos;
    7. this.delegate = delegate;
    8. }
    9. @Override
    10. public void put(K key, V val) {
    11. keyProp.put(key, cacheItemProperties());
    12. this.delegate.put(key, val);
    13. }
    14. @Override
    15. public V get(K key) {
    16. if (keyProp.get(key) != null && isExpire(keyProp.get(key))) {
    17. this.keyProp.remove(key);
    18. this.delegate.remove(key);
    19. return null;
    20. }
    21. return this.delegate.get(key);
    22. }
    23. @Override
    24. public V get(K key, Callable call) throws Exception {
    25. V cachedValue = this.get(key);
    26. if (null == cachedValue) {
    27. V v2 = call.call();
    28. this.put(key, v2);
    29. return v2;
    30. }
    31. return cachedValue;
    32. }
    33. @Override
    34. public V remove(K key) {
    35. keyProp.remove(key);
    36. return this.delegate.remove(key);
    37. }
    38. @Override
    39. public void clear() {
    40. keyProp.clear();
    41. this.delegate.clear();
    42. }
    43. @Override
    44. public int getSize() {
    45. return this.delegate.getSize();
    46. }
    47. private boolean isExpire(CacheItemProperties itemProperties) {
    48. if (itemProperties == null) {
    49. return true;
    50. }
    51. return expireNanos != -1 && (System.nanoTime() - itemProperties.getExpireNanos() > expireNanos);
    52. }
    53. private CacheItemProperties cacheItemProperties() {
    54. CacheItemProperties cacheItemProperties = new CacheItemProperties();
    55. cacheItemProperties.setExpireNanos(System.nanoTime());
    56. return cacheItemProperties;
    57. }
    58. }

  • 相关阅读:
    JavaScript中的数值
    IDEA常用快捷键总结(Windows)
    vTaskDelay()函数(ms级别)
    基于SSM的学生疫情信息管理系统设计与实现
    Java面向对象编程
    基于java的动漫网购商城【原创】
    PySide创建界面关联项目(五) 百篇文章学PyQT
    基于JAVA医院门诊预约系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    【C语言】【数据结构】【顺序表】
    【Golang】创建有配置参数的结构体时,可选参数应该怎么传?
  • 原文地址:https://blog.csdn.net/shadow_zed/article/details/128116158