如何通过保存方法的返回值来避免多次调用该方法?

huangapple go评论76阅读模式
英文:

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&lt;String, String&gt; storedPasswords = new HashMap&lt;&gt;();

  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(&quot;keyPassDataBasePath&quot;))
        .openDatabase(new File(config.getProperty(&quot;keyPassKeyPath&quot;)));
  
      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(&quot;accountName1&quot;);
    pwd2 = cachedCreds.getCredentials(&quot;accountName2&quot;);
}

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.

huangapple
  • 本文由 发表于 2020年8月13日 00:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63381273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定