added a service to handle the caching, this'll ensure the cache works while no activity is around, which is better for k9mail integration

it also is a more efficient and much smarter cache, not requiring an own timer thread, just a service that sleeps must of the time, it also is more accurate in cleaning up the entries, ensuring that the worst case of too late removal is 5 seconds
This commit is contained in:
Thialfihar
2010-06-03 16:17:55 +00:00
parent 371dc31b97
commit 600b44b9fc
5 changed files with 101 additions and 32 deletions

View File

@@ -225,19 +225,30 @@ public class Apg {
return cpp.passPhrase;
}
public static void cleanUpCache(int ttl) {
public static int cleanUpCache(int ttl, int initialDelay) {
int delay = initialDelay;
long realTtl = ttl * 1000;
long now = new Date().getTime();
Vector<Long> oldKeys = new Vector<Long>();
for (Map.Entry<Long, CachedPassPhrase> pair : mPassPhraseCache.entrySet()) {
if ((now - pair.getValue().timestamp) >= 1000 * ttl) {
long lived = now - pair.getValue().timestamp;
if (lived >= realTtl) {
oldKeys.add(pair.getKey());
} else {
// see, whether the remaining time for this cache entry improves our
// check delay
long nextCheck = realTtl - lived + 1000;
if (nextCheck < delay) {
delay = (int)nextCheck;
}
}
}
for (long keyId : oldKeys) {
mPassPhraseCache.remove(keyId);
}
return delay;
}
public static PGPSecretKey createKey(Context context,