如何实现条目自动过期的CacheMap? [英] How to Implement CacheMap with automatic expiration of entries?

查看:56
本文介绍了如何实现条目自动过期的CacheMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想在Java中实现缓存映射,其中映射项在给定时间后过期.

Hi everyone I want to implement cache map in java in which map entries expire after given time.

我有这样的界面,我必须实现这些方法,但是我不知道该如何真正开始.

I have interface like this, I have to implement these methods, but I am not understand how actually start.

    public class CacheMapImpl implements CacheMap<Integer, String> {

    @Override
    public void setTimeToLive(long timeToLive) {


    }

    @Override
    public long getTimeToLive() {

        return 0;
    }

    @Override
    public String put(Integer key, String value) {

        return null;
    }

    @Override
    public void clearExpired() {


    }

    @Override
    public void clear() {


    }

    @Override
    public boolean containsKey(Object key) {

        return false;
    }

    @Override
    public boolean containsValue(Object value) {

        return false;
    }

    @Override
    public String get(Object key) {

        return null;
    }

    @Override
    public boolean isEmpty() {

        return false;
    }

    @Override
    public String remove(Object key) {

        return null;
    }

    @Override
    public int size() {

        return 0;
    }

}

请告诉我如何实现这些方法,如何开始为我编写一些代码,并用代码更新我的cachemap接口.

Please tell me how to implement these methods, how to start write little bit code for me, kindly update my cachemap interface with code.

推荐答案

您必须使用相同的密钥来管理内部映射.使用put方法可以将新值添加到地图中,还可以为内部时间地图添加值.您可以将Long存储为一个值,这是该值的具体时间.

You have to manage an internal map with the same key. User your put method to add the new value to your map and also add a value for your internal times map. You can store a Long as a value, which is the concrete time for that value.

然后,在后台启动一个新线程,它将在所有时间检查内部映射中的所有键,并从内部映射和主映射中删除均为旧"项的键.

Then, start a new thread in background thatc will check all times for all keys in the internal map and remove those that are 'old' entries from both, internal map and your main map.

这是代码.当我看到您的Map实现了一个接口,其中提供了一些用于清除过期值的方法时,我知道您不需要自动方法来删除过期值.因此,代码应类似于:

Here is the code. As I see your Map implements an interface with some methods provided for clear the expired values, I understand you don't need an automatic way to remove expired values. So, the code should be something like:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class CacheMapImpl implements CacheMap<Integer, String> {

    private Map<Integer, Long> timesCache = new HashMap<Integer, Long>();
    private Map<Integer, String> values = new HashMap<Integer, String>();

    /** Time for the elemens to keep alive in the map in milliseconds. */
    long timeToLive = 0;

    @Override
    public void setTimeToLive(long timeToLive) {
        this.timeToLive = timeToLive;
    }

    @Override
    public long getTimeToLive() {

        return this.timeToLive;
    }

    @Override
    public String put(Integer key, String value) {
        values.put(key, value);
        timesCache.put(key, System.currentTimeMillis());
        return value;
    }

    @Override
    public void clearExpired() {

        // Just remove if timeToLive has been set before...
        if (timeToLive > 0) {
            List<Integer> keysToClear = new ArrayList<Integer>();
            long currentTime = System.currentTimeMillis();

            // Check what keys to remove
            for (Entry<Integer, Long> e : timesCache.entrySet()) {
                if ((currentTime - e.getValue().longValue()) > this.timeToLive) {
                    keysToClear.add(e.getKey());
                }
            }

            // Remove the expired keys
            for (Integer key : keysToClear) {
                this.timesCache.remove(key);
                this.values.remove(key);
            }
        }

    }

    @Override
    public void clear() {
        this.timesCache.clear();
        this.values.clear();
    }

    @Override
    public boolean containsKey(Object key) {

        return this.values.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {

        return this.values.containsValue(value);
    }

    @Override
    public String get(Object key) {

        return this.values.get(key);
    }

    @Override
    public boolean isEmpty() {

        return this.values.isEmpty();
    }

    @Override
    public String remove(Object key) {
        String rto = null;
        if (containsKey(key)) {
            this.values.remove(key);
            this.timesCache.remove(key);
            rto = key.toString();
        }
        return rto;
    }

    @Override
    public int size() {

        return this.values.size();
    }

}

这篇关于如何实现条目自动过期的CacheMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆