zoukankan      html  css  js  c++  java
  • 自定义缓存设计(static缓存数据)

    设计题

      编程过程中,为了解决缓存数据共享的问题,常常会使用static关键字达到脱离具体实例化对象,在整个java进程生命周期内共享数据的目的。请编写一个类,类名为MapCache,拥有但不局限于以下属性及方法:

    a) 静态的,类型为HashMap的成员变量,用于存储缓存数据
    b) 方法名为putCache的方法,实现缓存数据功能
    c) 方名为getCache的方法,实现获取缓存数据功能

    该类可以实现在不同的java实例中,存储缓存数据和获取缓存数据的功能。

     1 import java.util.HashMap;
     2 import java.util.Map;
     3 public class MapCache {
     4      private static final Map<Object,Object> cache = new HashMap<Object,Object>();
     5     
     6      public static void putCache(Object key, Object value){
     7           cache.put(key, value);
     8      }
     9      public static Object getCache(Object key){
    10           return cache.get(key);
    11      }
    12      public static boolean containsCache(Object key){
    13           return cache.containsKey(key);
    14      }
    15      public static int size(){
    16           return cache.size();
    17      }
    18      public static void clearCache(Object key){
    19           cache.remove(key);
    20      }
    21     
    22      public static void clearAllCache(){
    23           cache.clear();
    24      }
    25 }
  • 相关阅读:
    NSIS制作安装程序
    poj_1011木棒
    hdoj_1312Red and Black
    搜索题目推荐及解题报告
    应届生就职前要读的几本书
    poj_1564Sum It Up
    priority_queue用法
    hdoj_2952Counting Sheep
    poj_1154LETTERS
    poj_2362
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/customCache.html
Copyright © 2011-2022 走看看