From: Francesco Chemolli Date: Tue, 1 Sep 2015 12:30:26 +0000 (+0200) Subject: Initial methods from Auth::UserNameCache implemented X-Git-Tag: SQUID_4_0_1~21^2~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f21d7eaae92a5b853ed9d04f9866cd1cd75cb0bf;p=thirdparty%2Fsquid.git Initial methods from Auth::UserNameCache implemented --- diff --git a/src/auth/Makefile.am b/src/auth/Makefile.am index 45bee785a7..f12dedda6e 100644 --- a/src/auth/Makefile.am +++ b/src/auth/Makefile.am @@ -32,6 +32,8 @@ libauth_la_SOURCES = \ State.cc \ User.h \ User.cc \ + UserNameCache.h \ + UserNameCache.cc \ UserRequest.h \ UserRequest.cc diff --git a/src/auth/UserNameCache.cc b/src/auth/UserNameCache.cc new file mode 100644 index 0000000000..ba28ae4643 --- /dev/null +++ b/src/auth/UserNameCache.cc @@ -0,0 +1,51 @@ +/* + * Copyright (C) 1996-2015 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#include "squid.h" +#include "SquidConfig.h" +#include "SquidTime.h" +#include "UserNameCache.h" + +#include + +namespace Auth { + +User::Pointer +UserNameCache::lookup(const SBuf &userKey) +{ + auto p = store_.find(userKey); + if (p == store_.end()) + return User::Pointer(); + return p->second; +} + +void +UserNameCache::reset() +{ + store_.clear(); +} + +size_t +UserNameCache::size() +{ + return store_.size(); +} + +void +UserNameCache::cleanup(void *) +{ + // cache entries with expiretime <= expirationTime are to be evicted + const time_t expirationTime = current_time.tv_sec - ::Config.authenticateTTL; + const auto end = store_.end(); + for (auto i = store_.begin(); i != end; ++i) { + if (i->second->expiretime <= expirationTime) + store_.erase(i); + } +} + +} /* namespace Auth */ diff --git a/src/auth/UserNameCache.h b/src/auth/UserNameCache.h new file mode 100644 index 0000000000..86274ca6ed --- /dev/null +++ b/src/auth/UserNameCache.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 1996-2015 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#ifndef SQUID_USERNAMECACHE_H_ +#define SQUID_USERNAMECACHE_H_ + +#include "SBufAlgos.h" +#include "User.h" + +#include + +namespace Auth { + +class UserNameCache +{ +private: + /// key is Uer::userKey(), mapped value is User::Pointer + typedef std::unordered_map StoreType; + +public: + UserNameCache() = delete; + explicit UserNameCache(const char *name) : cachename(name) {} + + ~UserNameCache() = default; + UserNameCache(const UserNameCache& ) = delete; + UserNameCache& operator=(const UserNameCache&) = delete; + + /// obtain pointer to user if present, or Pointer(nullptr) if not + User::Pointer lookup(const SBuf &userKey); + + void reset(); + + size_t size(); + + /// periodic cleanup function, removes timed-out entries. + void cleanup(void *); + +private: + StoreType store_; + + // for logs, events etc. + const char *cachename; +}; + +} /* namespace Auth */ +#endif /* SQUID_USERNAMECACHE_H_ */