<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ゞ沉默是金ゞ

    魚離不開水,但是沒有說不離開哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    The processing costs for selecting a value from a database-table are fairly high compared to the costs having the value already in memory. So it seems preferrable to use some smart caching-mechanism that keeps often used values in your application instead of retrieving these values from resources somewhere ‘outside’.

    Most frameworks have at least one cache implementation onboard, but there also exist several other implementations of caches like e.g. EHCache. Even ordinary HashMaps/Hashtables can serve as caches also.

    A critial factor when using caches in Java is the size of the cache: when your cache grows too big, the Java Garbage Collector has to cleanup more often (which consumes time) or your application even crashes with a java.lang.OutOfMemoryError.

    One way to control the memory-consumption of caches is to use SoftReferences in HashMaps/Hashtables, another one is to throw away old or unused content by implementing a caching-strategy like e.g. LRU.

    A simple LRU-cache already ships within the components of the Java Standard Library: the LinkedHashMap. All you have to do is to tell your application whether the eldest entry in the map should be retained or removed after a new entry is inserted. Additionally a special constructor has to be used that defines the orderingMode for the map: ‘true’ for access-order (LRU), ‘false’ for insertion-order.

    Suppose we want to cache a mapping of String-Names to String-Ids with a maximum size of 20480 entries.
    How that can be done is shown by the example below with the use of an Anonymous Inner Class that overrides the removeEldestEntry-method of the LinkedHashMap.

    import com.fmr.pzn.wmb.utils.Vaidator;

    import java.util.Collections;
    import java.util.LinkedHashMap;
    import java.util.Map;

    public final class SampleCache {

        private static final float LOAD_FACTOR = 0.75f;

        private static final int CACHE_MAX = 20480;

        private static final int CACHE_INIT = 10000;

        private static final LinkedHashMap<String, String> CACHE_MAP = new LinkedHashMap<String, String>(CACHE_INIT,
                LOAD_FACTOR, true) {

            private static final long serialVersionUID = 628057782352519437L;

            @Override
            protected boolean removeEldestEntry(Entry<String, String> eldest) {
                return size() > SampleCache.CACHE_MAX;
            }

        };


        private SampleCache() {
            super();
        }

        public static void putid(final String id, final String name) {
            if (isEmpty(id) || isEmpty(name)) {
                return;
            }

            CACHE_MAP.put(id, name);
        }

        public static String getByid(final String id) {
            return CACHE_MAP.get(id);
        }

        public static boolean isEmpty(final String field ){
            boolean isEmpty = false;
            if(field == null || field.isEmpty())
            {
                isEmpty = true;
            }
            return isEmpty;
        }
    }
    And based on the high performance environment, this cache will not thread safe. How to fix this small issue? we can use Collections.synchronizedMap() to synchronized cache. like:

        private static final Map<String, String> CACHE_MAP = Collections.synchronizedMap(new LinkedHashMap<String, String>(CACHE_INIT,
                LOAD_FACTOR, true) {

            private static final long serialVersionUID = 628057782352519437L;

            @Override
            protected boolean removeEldestEntry(Entry<String, String> eldest) {
                return size() > SampleCache.CACHE_MAX;
            }

        });

    Now, there is one small cache exists in your application.
    posted on 2012-08-07 16:11 ゞ沉默是金ゞ 閱讀(428) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 在线永久看片免费的视频| 国产免费久久精品99久久| 久久久综合亚洲色一区二区三区 | 亚洲国产日韩a在线播放| 免费A级毛片无码视频| 国产成人亚洲精品狼色在线| 亚洲高清在线视频| eeuss免费天堂影院| 亚洲精品无码久久久久AV麻豆| 亚洲va在线va天堂va不卡下载| 国偷自产一区二区免费视频| 亚洲一区二区三区无码中文字幕| 久久亚洲春色中文字幕久久久| a级毛片黄免费a级毛片| 亚洲一区二区三区自拍公司| 黄色网址在线免费| 国产精品亚洲综合五月天| 亚洲av纯肉无码精品动漫| 亚洲老熟女五十路老熟女bbw| 欧洲一级毛片免费| 亚洲成a人不卡在线观看| 国产美女视频免费观看的网站| 亚洲av无码国产精品色在线看不卡| 国产精品亚洲一区二区无码| xvideos亚洲永久网址| 国产VA免费精品高清在线| 亚洲成a人片77777kkkk| 苍井空亚洲精品AA片在线播放| 免费人成激情视频| 亚洲av中文无码乱人伦在线观看| 国产成人免费福利网站| a一级爱做片免费| 97亚洲熟妇自偷自拍另类图片 | 成人精品国产亚洲欧洲| 亚洲精品线路一在线观看| 四虎影视在线影院在线观看免费视频| 亚洲人成电影青青在线播放| 免费成人在线观看| 污污网站免费观看| 国产亚洲视频在线| 亚洲精品午夜视频|