move UserAccountCache access locking to its methods and not callers.

melanie
UbitUmarov 2016-11-09 20:09:49 +00:00
parent 924c5fb55e
commit 1e1d0d8204
3 changed files with 54 additions and 55 deletions

View File

@ -154,14 +154,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
{ {
bool inCache = false; bool inCache = false;
UserAccount account; UserAccount account;
lock(m_Cache) account = m_Cache.Get(userID, out inCache);
account = m_Cache.Get(userID, out inCache);
if (inCache) if (inCache)
return account; return account;
account = UserAccountService.GetUserAccount(scopeID, userID); account = UserAccountService.GetUserAccount(scopeID, userID);
lock(m_Cache) m_Cache.Cache(userID, account);
m_Cache.Cache(userID, account);
return account; return account;
} }
@ -170,15 +168,13 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
{ {
bool inCache = false; bool inCache = false;
UserAccount account; UserAccount account;
lock(m_Cache) account = m_Cache.Get(firstName + " " + lastName, out inCache);
account = m_Cache.Get(firstName + " " + lastName, out inCache);
if (inCache) if (inCache)
return account; return account;
account = UserAccountService.GetUserAccount(scopeID, firstName, lastName); account = UserAccountService.GetUserAccount(scopeID, firstName, lastName);
if (account != null) if (account != null)
lock(m_Cache) m_Cache.Cache(account.PrincipalID, account);
m_Cache.Cache(account.PrincipalID, account);
return account; return account;
} }
@ -201,8 +197,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
{ {
if(UUID.TryParse(id, out uuid)) if(UUID.TryParse(id, out uuid))
{ {
lock(m_Cache) account = m_Cache.Get(uuid, out inCache);
account = m_Cache.Get(uuid, out inCache);
if (inCache) if (inCache)
ret.Add(account); ret.Add(account);
else else
@ -220,8 +215,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
foreach(UserAccount acc in ext) foreach(UserAccount acc in ext)
{ {
if(acc != null) if(acc != null)
lock(m_Cache) m_Cache.Cache(acc.PrincipalID, acc);
m_Cache.Cache(acc.PrincipalID, acc);
} }
} }
return ret; return ret;

View File

@ -128,8 +128,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
// flags, title, etc. And country, don't forget country! // flags, title, etc. And country, don't forget country!
private void OnNewClient(IClientAPI client) private void OnNewClient(IClientAPI client)
{ {
lock(m_Cache) m_Cache.Remove(client.Name);
m_Cache.Remove(client.Name);
} }
#region Overwritten methods from IUserAccountService #region Overwritten methods from IUserAccountService
@ -138,17 +137,14 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
{ {
bool inCache = false; bool inCache = false;
UserAccount account; UserAccount account;
lock(m_Cache) account = m_Cache.Get(userID, out inCache);
account = m_Cache.Get(userID, out inCache);
if (inCache) if (inCache)
return account; return account;
account = base.GetUserAccount(scopeID, userID); account = base.GetUserAccount(scopeID, userID);
if(account != null) if(account != null)
{ m_Cache.Cache(userID, account);
lock(m_Cache)
m_Cache.Cache(userID, account);
}
return account; return account;
} }
@ -156,17 +152,14 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
{ {
bool inCache = false; bool inCache = false;
UserAccount account; UserAccount account;
lock(m_Cache) account = m_Cache.Get(firstName + " " + lastName, out inCache);
account = m_Cache.Get(firstName + " " + lastName, out inCache);
if (inCache) if (inCache)
return account; return account;
account = base.GetUserAccount(scopeID, firstName, lastName); account = base.GetUserAccount(scopeID, firstName, lastName);
if (account != null) if (account != null)
{ m_Cache.Cache(account.PrincipalID, account);
lock(m_Cache)
m_Cache.Cache(account.PrincipalID, account);
}
return account; return account;
} }
@ -183,8 +176,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
{ {
if(UUID.TryParse(id, out uuid)) if(UUID.TryParse(id, out uuid))
{ {
lock(m_Cache) account = m_Cache.Get(uuid, out inCache);
account = m_Cache.Get(uuid, out inCache);
if (inCache) if (inCache)
accs.Add(account); accs.Add(account);
else else
@ -202,8 +194,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
if(acc != null) if(acc != null)
{ {
accs.Add(acc); accs.Add(acc);
lock(m_Cache) m_Cache.Cache(acc.PrincipalID, acc);
m_Cache.Cache(acc.PrincipalID, acc);
} }
} }
} }

View File

@ -44,6 +44,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
private ExpiringCache<UUID, UserAccount> m_UUIDCache; private ExpiringCache<UUID, UserAccount> m_UUIDCache;
private ExpiringCache<string, UUID> m_NameCache; private ExpiringCache<string, UUID> m_NameCache;
private object accessLock = new object();
public UserAccountCache() public UserAccountCache()
{ {
@ -54,60 +55,73 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts
public void Cache(UUID userID, UserAccount account) public void Cache(UUID userID, UserAccount account)
{ {
// Cache even null accounts // Cache even null accounts
m_UUIDCache.AddOrUpdate(userID, account, CACHE_EXPIRATION_SECONDS); lock(accessLock)
if (account != null) {
m_NameCache.AddOrUpdate(account.Name, account.PrincipalID, CACHE_EXPIRATION_SECONDS); m_UUIDCache.AddOrUpdate(userID, account, CACHE_EXPIRATION_SECONDS);
if (account != null)
m_NameCache.AddOrUpdate(account.Name, account.PrincipalID, CACHE_EXPIRATION_SECONDS);
//m_log.DebugFormat("[USER CACHE]: cached user {0}", userID); //m_log.DebugFormat("[USER CACHE]: cached user {0}", userID);
}
} }
public void Invalidate(UUID userID) public void Invalidate(UUID userID)
{ {
m_UUIDCache.Remove(userID); lock(accessLock)
m_UUIDCache.Remove(userID);
} }
public UserAccount Get(UUID userID, out bool inCache) public UserAccount Get(UUID userID, out bool inCache)
{ {
UserAccount account = null; UserAccount account = null;
inCache = false; inCache = false;
if (m_UUIDCache.TryGetValue(userID, out account)) lock(accessLock)
{ {
//m_log.DebugFormat("[USER CACHE]: Account {0} {1} found in cache", account.FirstName, account.LastName); if (m_UUIDCache.TryGetValue(userID, out account))
inCache = true; {
return account; //m_log.DebugFormat("[USER CACHE]: Account {0} {1} found in cache", account.FirstName, account.LastName);
inCache = true;
return account;
}
} }
return null; return null;
} }
public UserAccount Get(string name, out bool inCache) public UserAccount Get(string name, out bool inCache)
{ {
inCache = false; inCache = false;
if (!m_NameCache.Contains(name)) lock(accessLock)
return null; {
if (!m_NameCache.Contains(name))
return null;
UserAccount account = null; UserAccount account = null;
UUID uuid = UUID.Zero; UUID uuid = UUID.Zero;
if (m_NameCache.TryGetValue(name, out uuid)) if (m_NameCache.TryGetValue(name, out uuid))
if (m_UUIDCache.TryGetValue(uuid, out account))
{ {
inCache = true; if (m_UUIDCache.TryGetValue(uuid, out account))
return account; {
inCache = true;
return account;
}
} }
}
return null; return null;
} }
public void Remove(string name) public void Remove(string name)
{ {
if (!m_NameCache.Contains(name)) lock(accessLock)
return;
UUID uuid = UUID.Zero;
if (m_NameCache.TryGetValue(name, out uuid))
{ {
m_NameCache.Remove(name); if (!m_NameCache.Contains(name))
m_UUIDCache.Remove(uuid); return;
UUID uuid = UUID.Zero;
if (m_NameCache.TryGetValue(name, out uuid))
{
m_NameCache.Remove(name);
m_UUIDCache.Remove(uuid);
}
} }
} }
} }