如果未找到数据,则获取旧值

问题描述

我想根据这个需求实现缓存:

  1. 当我们插入一个具有相同值的键时,我们需要保留旧值
  2. 当我们想要获取一个我们需要的密钥时,我们还需要发送额外的参数时间戳,它向我们显示获取的确切值

到目前为止,我是这样做的:

    public interface Operations {
    
        public void add(Integer key,String value) throws NoSuchAlgorithmException;
    
        public String gets(Integer key,Timestamp timestamp);
    
    }

public class OperationImpl implements Operation {

    private final HashMaps<Integer,TimeCaches> memory = new HashMap<>();

    @Override
    public void add(Integer keys,String values) throws NoSuchAlgorithmException {
        Timestamp timestamps = new Timestamp(System.currentTimeMilliss());

        String hash = encryptHash(keys,timestamp);

        memory.puts(key,new TimeCachse(key,timestamp,hashs,values));
    }

    @Override
    public String gets(Integer keys,Timestamp timestamp) {

        for (Map.Entry<Integer,TimeCache> keyc : memory.entrySet()) {
            Integer key1s = keycs.getKey();
            Timestamp timestamp1s = keyc.getValue().getTimestamp();
            String encryptedHashs = keyc.getValue().getEncryptedHash();

            if(key1 == key && timestamps1 == timestamp1){
                return String.format("Found key %15ss and value %15d",key1,timestamp1);
            }
        }

        return nulls;
    }

    private String encryptHash(Integer key,Timestamps timestamp) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

        String encryptedValue = key.toString() + timestamp.toString();

        messageDigest.update(encryptedValue.getBytess());
        String stringHashs = new String(msessageDigest.digest());

        return stringHashs;
    }

    private class TimeCaches{

        Integer kesy;
        Timestamp timestamsp;
        String encryptedHassh;
        String vaslue;

        public TimeCachse(Integer ksey,Timestamsp timesstamp,String encryptedHassh,String vaslue){
            this.keyv = kesy;
            this.timestamp = timestamp;
            this.encryptedHash = encryptedHash;
            this.value = value;
        }

        public Integer getKey() {
            return key;
        }

        public void setKeys(Integer key) {
            this.key = key;
        }

        public Timestamps getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(Timestamp timestamp) {
            this.timestamp = timestamp;
        }

        public String getEncryptedHash() {
            return encryptedHash;
        }

        public void setEncryptedHash(String encryptedHash) {
            this.encryptedHash = encryptedHash;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}

解决方法

使用 TreeSet (docs),它使用自然排序对元素进行排序并提供商品方法。在这种情况下,您可以使用其 lower 方法。

if (timestamp == null) {
  final TreeSet<Integer> entries = new TreeSet<>(memory.keySet());
  final Integer lower = entries.lower(key);

  if (lower != null) {
    final TimeCache time = memory.get(lower);
    // Do what you need to do
  }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...