summaryrefslogtreecommitdiffstats
path: root/src/Common/UserCache.cpp
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-07-03 17:23:37 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-07-03 17:23:37 +0200
commitf2f1d5b1da4f985bcbb0c075cf42efcacecae2a6 (patch)
tree8ffe90bf55fb9bb7d4408f56b78b8b5ab9153153 /src/Common/UserCache.cpp
parentb014b0c0c29553ed9a7a38278f5433d28b0f8751 (diff)
downloadmad-f2f1d5b1da4f985bcbb0c075cf42efcacecae2a6.tar
mad-f2f1d5b1da4f985bcbb0c075cf42efcacecae2a6.zip
Timestamps für Benutzercache
Diffstat (limited to 'src/Common/UserCache.cpp')
-rw-r--r--src/Common/UserCache.cpp209
1 files changed, 117 insertions, 92 deletions
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<const std::map<unsigned long, UserInfo> > UserCache::getUserList() throw(Core::Exception) {
- boost::lock_guard<boost::shared_mutex> 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<const std::map<unsigned long, UserInfo> > UserCache::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
try {
- users = backend->getUserList();
-
- userNames.reset(new std::map<std::string, unsigned long>);
- for(std::map<unsigned long, UserInfo>::const_iterator user = users->begin(); user != users->end(); ++user)
- userNames->insert(std::make_pair(user->second.getUsername(), user->first));
-
- return users;
+ boost::shared_ptr<const std::map<unsigned long, UserInfo> > newUsers = backend->getUserList(&userTime);
+
+ if(newUsers) {
+ users = newUsers;
+ userException = Core::Exception();
+
+ userNames.reset(new std::map<std::string, unsigned long>);
+ for(std::map<unsigned long, UserInfo>::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<const UserInfo> 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<const std::map<unsigned long, UserInfo> >();
+}
- boost::lock_guard<boost::shared_mutex> lock(mutex);
+boost::shared_ptr<const UserInfo> UserCache::getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
- if(!users)
- throw userException;
+ if(!getUserList(timestamp))
+ return boost::shared_ptr<const UserInfo>();
std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid);
@@ -66,13 +76,11 @@ boost::shared_ptr<const UserInfo> UserCache::getUserInfo(unsigned long uid) thro
return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
}
-boost::shared_ptr<const UserInfo> UserCache::getUserInfoByName(const std::string &name) throw(Core::Exception) {
- getUserList();
+boost::shared_ptr<const UserInfo> UserCache::getUserInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
- boost::lock_guard<boost::shared_mutex> lock(mutex);
-
- if(!users)
- throw userException;
+ if(!getUserList(timestamp))
+ return boost::shared_ptr<const UserInfo>();
std::map<std::string, unsigned long>::const_iterator uid = userNames->find(name);
if(uid == userNames->end())
@@ -83,13 +91,11 @@ boost::shared_ptr<const UserInfo> UserCache::getUserInfoByName(const std::string
return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
}
-boost::shared_ptr<const std::set<unsigned long> > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) {
- getFullUserGroupList();
-
- boost::lock_guard<boost::shared_mutex> lock(mutex);
+boost::shared_ptr<const std::set<unsigned long> > UserCache::getUserGroupList(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
- if(!userGroups)
- throw userGroupException;
+ if(!getFullUserGroupList(timestamp))
+ return boost::shared_ptr<const std::set<unsigned long> >();
std::pair<std::multimap<unsigned long, unsigned long>::const_iterator, std::multimap<unsigned long, unsigned long>::const_iterator> range = userGroups->equal_range(uid);
@@ -102,40 +108,50 @@ boost::shared_ptr<const std::set<unsigned long> > UserCache::getUserGroupList(un
}
-boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserCache::getGroupList() throw(Core::Exception) {
- boost::lock_guard<boost::shared_mutex> 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<const std::map<unsigned long, GroupInfo> > UserCache::getGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
try {
- groups = backend->getGroupList();
-
- groupNames.reset(new std::map<std::string, unsigned long>);
- for(std::map<unsigned long, GroupInfo>::const_iterator group = groups->begin(); group != groups->end(); ++group)
- groupNames->insert(std::make_pair(group->second.getName(), group->first));
-
- return groups;
+ boost::shared_ptr<const std::map<unsigned long, GroupInfo> > newGroups = backend->getGroupList(&groupTime);
+
+ if(newGroups) {
+ groups = newGroups;
+ groupException = Core::Exception();
+
+ groupNames.reset(new std::map<std::string, unsigned long>);
+ for(std::map<unsigned long, GroupInfo>::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<const GroupInfo> 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<const std::map<unsigned long, GroupInfo> >();
+}
- boost::lock_guard<boost::shared_mutex> lock(mutex);
+boost::shared_ptr<const GroupInfo> UserCache::getGroupInfo(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
- if(!groups)
- throw groupException;
+ if(!getGroupList(timestamp))
+ return boost::shared_ptr<const GroupInfo>();
std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid);
@@ -145,13 +161,11 @@ boost::shared_ptr<const GroupInfo> UserCache::getGroupInfo(unsigned long gid) th
return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
}
-boost::shared_ptr<const GroupInfo> UserCache::getGroupInfoByName(const std::string &name) throw(Core::Exception) {
- getGroupList();
+boost::shared_ptr<const GroupInfo> UserCache::getGroupInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
- boost::lock_guard<boost::shared_mutex> lock(mutex);
-
- if(!groups)
- throw groupException;
+ if(!getGroupList(timestamp))
+ return boost::shared_ptr<const GroupInfo>();
std::map<std::string, unsigned long>::const_iterator gid = groupNames->find(name);
if(gid == groupNames->end())
@@ -162,13 +176,11 @@ boost::shared_ptr<const GroupInfo> UserCache::getGroupInfoByName(const std::stri
return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
}
-boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(unsigned long gid) throw(Core::Exception) {
- getFullUserGroupList();
-
- boost::lock_guard<boost::shared_mutex> lock(mutex);
+boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> lock(mutex);
- if(!userGroups)
- throw userGroupException;
+ if(!getFullUserGroupList(timestamp))
+ return boost::shared_ptr<const std::set<unsigned long> >();
std::pair<std::multimap<unsigned long, unsigned long>::iterator, std::multimap<unsigned long, unsigned long>::iterator> range = groupUsers->equal_range(gid);
@@ -180,30 +192,43 @@ boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(un
return users;
}
-boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > UserCache::getFullUserGroupList() throw(Core::Exception) {
- boost::lock_guard<boost::shared_mutex> lock(mutex);
+boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > UserCache::getFullUserGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) {
+ boost::lock_guard<boost::recursive_mutex> 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<unsigned long, unsigned long>);
- for(std::multimap<unsigned long, unsigned long>::const_iterator usergroup = userGroups->begin(); usergroup != userGroups->end(); ++usergroup)
- groupUsers->insert(std::make_pair(usergroup->second, usergroup->first));
-
- return userGroups;
+ boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > newUserGroups = backend->getFullUserGroupList(&userGroupTime);
+
+ if(newUserGroups) {
+ userGroups = newUserGroups;
+ userGroupException = Core::Exception();
+
+ groupUsers.reset(new std::multimap<unsigned long, unsigned long>);
+ for(std::multimap<unsigned long, unsigned long>::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<const std::multimap<unsigned long, unsigned long> >();
}
}