Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2013-12-19 22:19:36 +0100
committerArthur de Jong <arthur@arthurdejong.org>2013-12-21 20:53:19 +0100
commit82bac6103d44313c834feb2d7a8046a75c3fa8f9 (patch)
tree58c8a8acbe0cbd09129d8cbcc9b0335082b31e11
parentb9ec6df4ab3b7a1fc16dc894f03f538dafb879fb (diff)
Have positive and negative cache timeouts
The positive value determines the time a found entry is valid, the negative timeout determines the lifetime of not found entries.
-rw-r--r--nslcd/passwd.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/nslcd/passwd.c b/nslcd/passwd.c
index 2ad53a3..bdc2049 100644
--- a/nslcd/passwd.c
+++ b/nslcd/passwd.c
@@ -162,7 +162,8 @@ struct dn2uid_cache_entry {
time_t timestamp;
char *uid;
};
-#define DN2UID_CACHE_TIMEOUT (15 * 60)
+#define DN2UID_CACHE_POSITIVE_TIMEOUT (30 * 60)
+#define DN2UID_CACHE_NEGATIVE_TIMEOUT (15 * 60)
/* checks whether the entry has a valid uidNumber attribute
(>= nss_min_uid) */
@@ -285,17 +286,25 @@ char *dn2uid(MYLDAP_SESSION *session, const char *dn, char *buf, size_t buflen)
dn2uid_cache = dict_new();
if ((dn2uid_cache != NULL) && ((cacheentry = dict_get(dn2uid_cache, dn)) != NULL))
{
- /* if the cached entry is still valid, return that */
- if (time(NULL) < (cacheentry->timestamp + DN2UID_CACHE_TIMEOUT))
+ if ((cacheentry->uid != NULL) && (strlen(cacheentry->uid) < buflen))
{
- if ((cacheentry->uid != NULL) && (strlen(cacheentry->uid) < buflen))
+ /* if the cached entry is still valid, return that */
+ if (time(NULL) < (cacheentry->timestamp + DN2UID_CACHE_POSITIVE_TIMEOUT))
+ {
strcpy(buf, cacheentry->uid);
- else
- buf = NULL;
- pthread_mutex_unlock(&dn2uid_cache_mutex);
- return buf;
+ pthread_mutex_unlock(&dn2uid_cache_mutex);
+ return buf;
+ }
+ }
+ else
+ {
+ if (time(NULL) < (cacheentry->timestamp + DN2UID_CACHE_NEGATIVE_TIMEOUT))
+ /* if the cached entry is still valid, return that */
+ {
+ pthread_mutex_unlock(&dn2uid_cache_mutex);
+ return NULL;
+ }
}
- /* leave the entry intact, just replace the uid below */
}
pthread_mutex_unlock(&dn2uid_cache_mutex);
/* look up the uid using an LDAP query */