英文:
How to avoid several calls to a method by saving its returning value?
问题
Sure, here's the translated code portion:
public String getCredentials(String entry) throws IOException {
    KeePassFile database = KeePassDatabase
            .getInstance(config.getProperty("keyPassDataBasePath"))
            .openDatabase(new File(config.getProperty("keyPassKeyPath")));
    Entry sampleEntry = database.getEntryByTitle(entry);
    return sampleEntry.getPassword();
}
这是我的方法,基本上是访问 KeePass 数据库,根据所属帐户的标题检索密码。
有很多需要两个密码的方法,因此会使用两个条目。
我不想每次调用该方法,因为我认为这是浪费资源。
如何保存返回的值,并在其他需要这些值的方法中使用呢?
这样会起作用吗?我觉得这会使方法多次调用。
private static String pwd1;
private static String pwd2;
public void setValues() throws IOException {
    pwd1 = getCredentials("accountName1");
    pwd2 = getCredentials("accountName2");
}
public String getPwd1(){
    return pwd1;
}
public String getPwd2(){
    return pwd2;
}
Please note that I have removed the HTML entities (") and used regular double quotation marks in the translated code.
英文:
I have this method
public String getCredentials(String entry) throws IOException {
    KeePassFile database = KeePassDatabase
            .getInstance(config.getProperty("keyPassDataBasePath"))
            .openDatabase(new File(config.getProperty("keyPassKeyPath")));
    Entry sampleEntry = database.getEntryByTitle(entry);
    return sampleEntry.getPassword();
}
Which is basically going to a KeePass DB, retrieves a password according to the title of its belonging account.
There are a LOT of methods that need 2 passwords, so 2 entries are used.
I don't want to call that method everytime as I think it's a waste of resources.
How can I save the returning value, and use it in other classes where the methods need those values?
Would this work? I feel like it's gonna make the method call several times anyways
    private static String pwd1;
    private static String pwd2;
    public void setValues() throws IOException {
        pwd1 = getCredentials("accountName1");
        pwd2 = getCredentials("accountName2");
    }
    public String getPwd1(){
        return pwd1;
    }
    public String getPwd2(){
        return pwd2;
    }
答案1
得分: 1
将它们存储在一个HashMap中,其中键是条目,密码是值:
class CachedCredentials {
  private Map<String, String> storedPasswords = new HashMap<>();
  private Properties config;
  
  public CachedCredentials(Properties config) {
     this.config = config;
  }
  
  public String getCredentials(String entry) {
    if (!storedPasswords.containsKey(entry)) {
      KeePassFile database = KeePassDatabase
        .getInstance(config.getProperty("keyPassDataBasePath"))
        .openDatabase(new File(config.getProperty("keyPassKeyPath")));
  
      Entry sampleEntry = database.getEntryByTitle(entry);   
      storedPasswords.put(entry, sampleEntry.getPassword());
    }
    return storedPasswords.get(entry);
  }
}
然后在你的setValues方法中,你可以这样做:
private CachedCredentials cachedCreds; // 在你的构造函数中初始化这个
public void setValues() throws IOException {
    pwd1 = cachedCreds.getCredentials("accountName1");
    pwd2 = cachedCreds.getCredentials("accountName2");
}
这个解决方案在程序运行时如果有人进行内存窃听可能会不安全。你可能需要考虑一种方法来模糊缓存的密码,可以使用base64编码或者实际加密,但这超出了问题的范围。
英文:
Store them in a HasMap with the key being the entry and the password being the value:
class CachedCredentials {
  private Map<String, String> storedPasswords = new HashMap<>();
  private Properties config;
  
  public CachedCredentials(Properties config) {
     this.config = config;
  }
  
  public String getCredentials(String entry) {
    if (!storedPasswords.containsKey(entry)) {
      KeePassFile database = KeePassDatabase
        .getInstance(config.getProperty("keyPassDataBasePath"))
        .openDatabase(new File(config.getProperty("keyPassKeyPath")));
  
      Entry sampleEntry = database.getEntryByTitle(entry);   
      storedPasswords.put(entry, sampleEntry.getPassword());
    }
    return storedPasswords.get(entry);
  }
then in your setValues method you can do:
private cachedCreds; //initialize this in your constructor
public void setValues() throws IOException {
    pwd1 = cachedCreds.getCredentials("accountName1");
    pwd2 = cachedCreds.getCredentials("accountName2");
}
This solution might provide to be insecure if someone is doing a memory snooping while the program is running. Might want to think of a way to obfuscate the cached password by either base64encoding it or actually encrypting it but that goes above what is being asked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论