/// <summary> /// get a user existing in database or throws an exception if user is not found /// </summary> /// <param name="service">service user is linked to</param> /// <param name="name">name of user</param> /// <returns>user object</returns> public User GetExistingUser(string service, string name) { lock (userlock) { UserKey key = new UserKey(service, name); UserCacheEntry cacheentry; usersbyname.TryGetValue(key, out cacheentry); if (cacheentry == null) { User user = context.Database.LoadEntities <User>().Where(u => u.Service == key.Service && u.Name == key.Username).Execute().FirstOrDefault(); if (user == null) { throw new Exception($"User '{name}' for service '{service}' not found."); } cacheentry = AddUserToCache(user); } cacheentry.LifeTime = 300.0; return(cacheentry.User); } }
/// <summary> /// get a user by service and username /// </summary> /// <param name="service">service user is registered to</param> /// <param name="name">username</param> /// <returns>user</returns> public User GetUser(string service, string name) { lock (userlock) { UserKey key = new UserKey(service, name); UserCacheEntry cacheentry; usersbyname.TryGetValue(key, out cacheentry); if (cacheentry == null) { User user = context.Database.LoadEntities <User>().Where(u => u.Service == key.Service && u.Name == key.Username).Execute().FirstOrDefault(); if (user == null) { user = new User { Service = service, Name = name, ID = context.Database.Insert <User>().Columns(u => u.Service, u => u.Name, u => u.Status).Values(service, name, UserStatus.Free).ReturnID().Execute() }; } cacheentry = AddUserToCache(user); } cacheentry.LifeTime = 300.0; return(cacheentry.User); } }