]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/CredentialsCache.h
Merged from trunk rev.14335
[thirdparty/squid.git] / src / auth / CredentialsCache.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_SRC_AUTH_CREDENTIALSCACHE_H
10 #define SQUID_SRC_AUTH_CREDENTIALSCACHE_H
11
12 #include "auth/User.h"
13 #include "cbdata.h"
14 #include "SBufAlgos.h"
15
16 #include <unordered_map>
17
18 namespace Auth {
19
20 /// Cache of Auth::User credentials, keyed by Auth::User::userKey
21 class CredentialsCache
22 {
23 private:
24 CBDATA_CLASS(CredentialsCache);
25
26 /// key is User::userKey(), mapped value is User::Pointer
27 typedef std::unordered_map<SBuf, Auth::User::Pointer> StoreType;
28
29 public:
30 explicit CredentialsCache(const char *name, const char * const eventName);
31
32 ~CredentialsCache() = default;
33 CredentialsCache(const CredentialsCache&) = delete;
34 CredentialsCache& operator=(const CredentialsCache&) = delete;
35
36 /// \returns a pointer to cached credentials, or nil if none found
37 Auth::User::Pointer lookup(const SBuf &userKey) const;
38
39 /// add an user to the cache with the provided key
40 void insert(const SBuf &userKey, Auth::User::Pointer anAuth_user);
41
42 /// clear cache
43 void reset() { store_.clear(); }
44
45 /// \returns number of cached usernames
46 size_t size() const { return store_.size(); }
47
48 /** periodic cleanup function, removes timed-out entries
49 *
50 * Must be static to support EVH interface. Argument will be this
51 */
52 static void Cleanup(void *);
53
54 /// cache garbage collection, removes timed-out entries
55 void cleanup();
56
57 /**
58 * Cleanup cache data after a reconfiguration has occured.
59 * Similar to cleanup() but also flushes stale config dependent
60 * state from retained entries.
61 */
62 void doConfigChangeCleanup();
63
64 /// \returns alphanumerically sorted list of usernames
65 std::vector<Auth::User::Pointer> sortedUsersList() const;
66
67 private:
68 void scheduleCleanup();
69
70 /// whether a cleanup (garbage collection) event has been scheduled
71 bool gcScheduled_;
72
73 StoreType store_;
74
75 // c-string raw pointer used as event name
76 const char * const cacheCleanupEventName;
77 };
78
79 } /* namespace Auth */
80
81 #endif /* SQUID_SRC_AUTH_CREDENTIALSCACHE_H */
82