From f2f1d5b1da4f985bcbb0c075cf42efcacecae2a6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 3 Jul 2009 17:23:37 +0200 Subject: =?UTF-8?q?Timestamps=20f=C3=BCr=20Benutzercache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/UserCache.cpp | 209 ++++++++++++++++++++++++++--------------------- 1 file changed, 117 insertions(+), 92 deletions(-) (limited to 'src/Common/UserCache.cpp') diff --git a/src/Common/UserCache.cpp b/src/Common/UserCache.cpp index 887e375..a32bcd3 100644 --- a/src/Common/UserCache.cpp +++ b/src/Common/UserCache.cpp @@ -23,40 +23,50 @@ namespace Mad { namespace Common { -boost::shared_ptr > UserCache::getUserList() throw(Core::Exception) { - boost::lock_guard lock(mutex); - - if(users) { - application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached user list"); - return users; - } - else if(userException) { - application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached"); - throw userException; - } +boost::shared_ptr > UserCache::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); try { - users = backend->getUserList(); - - userNames.reset(new std::map); - for(std::map::const_iterator user = users->begin(); user != users->end(); ++user) - userNames->insert(std::make_pair(user->second.getUsername(), user->first)); - - return users; + boost::shared_ptr > newUsers = backend->getUserList(&userTime); + + if(newUsers) { + users = newUsers; + userException = Core::Exception(); + + userNames.reset(new std::map); + for(std::map::const_iterator user = users->begin(); user != users->end(); ++user) + userNames->insert(std::make_pair(user->second.getUsername(), user->first)); + } + else { + if(users) + application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached user list"); + else + application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached"); + } } catch(Core::Exception e) { + users.reset(); userException = e; - throw userException; } -} -boost::shared_ptr UserCache::getUserInfo(unsigned long uid) throw(Core::Exception) { - getUserList(); + if((!timestamp) || timestamp->is_not_a_date_time() || (*timestamp < userTime)) { + if(timestamp) + *timestamp = userTime; + + if(users) + return users; + else + throw userException; + } + + return boost::shared_ptr >(); +} - boost::lock_guard lock(mutex); +boost::shared_ptr UserCache::getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - if(!users) - throw userException; + if(!getUserList(timestamp)) + return boost::shared_ptr(); std::map::const_iterator user = users->find(uid); @@ -66,13 +76,11 @@ boost::shared_ptr UserCache::getUserInfo(unsigned long uid) thro return boost::shared_ptr(new UserInfo(user->second)); } -boost::shared_ptr UserCache::getUserInfoByName(const std::string &name) throw(Core::Exception) { - getUserList(); +boost::shared_ptr UserCache::getUserInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - boost::lock_guard lock(mutex); - - if(!users) - throw userException; + if(!getUserList(timestamp)) + return boost::shared_ptr(); std::map::const_iterator uid = userNames->find(name); if(uid == userNames->end()) @@ -83,13 +91,11 @@ boost::shared_ptr UserCache::getUserInfoByName(const std::string return boost::shared_ptr(new UserInfo(user->second)); } -boost::shared_ptr > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) { - getFullUserGroupList(); - - boost::lock_guard lock(mutex); +boost::shared_ptr > UserCache::getUserGroupList(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - if(!userGroups) - throw userGroupException; + if(!getFullUserGroupList(timestamp)) + return boost::shared_ptr >(); std::pair::const_iterator, std::multimap::const_iterator> range = userGroups->equal_range(uid); @@ -102,40 +108,50 @@ boost::shared_ptr > UserCache::getUserGroupList(un } -boost::shared_ptr > UserCache::getGroupList() throw(Core::Exception) { - boost::lock_guard lock(mutex); - - if(groups) { - application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached group list"); - return groups; - } - else if(groupException) { - application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached"); - throw groupException; - } +boost::shared_ptr > UserCache::getGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); try { - groups = backend->getGroupList(); - - groupNames.reset(new std::map); - for(std::map::const_iterator group = groups->begin(); group != groups->end(); ++group) - groupNames->insert(std::make_pair(group->second.getName(), group->first)); - - return groups; + boost::shared_ptr > newGroups = backend->getGroupList(&groupTime); + + if(newGroups) { + groups = newGroups; + groupException = Core::Exception(); + + groupNames.reset(new std::map); + for(std::map::const_iterator group = groups->begin(); group != groups->end(); ++group) + groupNames->insert(std::make_pair(group->second.getName(), group->first)); + } + else { + if(groups) + application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached group list"); + else + application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached"); + } } catch(Core::Exception e) { + groups.reset(); groupException = e; - throw groupException; } -} -boost::shared_ptr UserCache::getGroupInfo(unsigned long gid) throw(Core::Exception) { - getGroupList(); + if((!timestamp) || timestamp->is_not_a_date_time() || (*timestamp < groupTime)) { + if(timestamp) + *timestamp = groupTime; + + if(groups) + return groups; + else + throw groupException; + } + + return boost::shared_ptr >(); +} - boost::lock_guard lock(mutex); +boost::shared_ptr UserCache::getGroupInfo(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - if(!groups) - throw groupException; + if(!getGroupList(timestamp)) + return boost::shared_ptr(); std::map::const_iterator group = groups->find(gid); @@ -145,13 +161,11 @@ boost::shared_ptr UserCache::getGroupInfo(unsigned long gid) th return boost::shared_ptr(new GroupInfo(group->second)); } -boost::shared_ptr UserCache::getGroupInfoByName(const std::string &name) throw(Core::Exception) { - getGroupList(); +boost::shared_ptr UserCache::getGroupInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - boost::lock_guard lock(mutex); - - if(!groups) - throw groupException; + if(!getGroupList(timestamp)) + return boost::shared_ptr(); std::map::const_iterator gid = groupNames->find(name); if(gid == groupNames->end()) @@ -162,13 +176,11 @@ boost::shared_ptr UserCache::getGroupInfoByName(const std::stri return boost::shared_ptr(new GroupInfo(group->second)); } -boost::shared_ptr > UserCache::getGroupUserList(unsigned long gid) throw(Core::Exception) { - getFullUserGroupList(); - - boost::lock_guard lock(mutex); +boost::shared_ptr > UserCache::getGroupUserList(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - if(!userGroups) - throw userGroupException; + if(!getFullUserGroupList(timestamp)) + return boost::shared_ptr >(); std::pair::iterator, std::multimap::iterator> range = groupUsers->equal_range(gid); @@ -180,30 +192,43 @@ boost::shared_ptr > UserCache::getGroupUserList(un return users; } -boost::shared_ptr > UserCache::getFullUserGroupList() throw(Core::Exception) { - boost::lock_guard lock(mutex); +boost::shared_ptr > UserCache::getFullUserGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { + boost::lock_guard lock(mutex); - if(userGroups) { - application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached user-group table"); - return userGroups; - } - else if(userGroupException) { - application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached"); - throw userGroupException; - } try { - userGroups = backend->getFullUserGroupList(); - - groupUsers.reset(new std::multimap); - for(std::multimap::const_iterator usergroup = userGroups->begin(); usergroup != userGroups->end(); ++usergroup) - groupUsers->insert(std::make_pair(usergroup->second, usergroup->first)); - - return userGroups; + boost::shared_ptr > newUserGroups = backend->getFullUserGroupList(&userGroupTime); + + if(newUserGroups) { + userGroups = newUserGroups; + userGroupException = Core::Exception(); + + groupUsers.reset(new std::multimap); + for(std::multimap::const_iterator usergroup = userGroups->begin(); usergroup != userGroups->end(); ++usergroup) + groupUsers->insert(std::make_pair(usergroup->second, usergroup->first)); + } + else { + if(userGroups) + application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached user-group table"); + else + application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached"); + } } catch(Core::Exception e) { + userGroups.reset(); userGroupException = e; - throw userGroupException; } + + if((!timestamp) || timestamp->is_not_a_date_time() || (*timestamp < userTime)) { + if(timestamp) + *timestamp = userGroupTime; + + if(userGroups) + return userGroups; + else + throw userGroupException; + } + + return boost::shared_ptr >(); } } -- cgit v1.2.3