Exemple #1
0
        public static void UpdateUser(string username)
        {
            var user = GetConfiguration(username);

            if (ConfigCacheHelper.UseDiscreteFiles)
            {
                lock (userCache)
                {
                    User oldUser;
                    userCache.TryRemove(username, out oldUser);
                    userCache.TryAdd(username, user);
                    var fileContent = JsonConvert.SerializeObject(user);
                    if (ConfigCacheHelper.EncryptFiles())
                    {
                        fileContent = fileContent.Encrypt(ConfigCacheHelper.LocalEncryptionKey);
                    }
                    File.WriteAllText(ConfigCacheHelper.GetLocalFileName(username, "user"), fileContent);
                }
            }
            else
            {
                ConfigCacheHelper.GetOrCreateConsolidatedFile();
                if (ConfigCacheHelper.consolidatedWrapper.Users.ContainsKey(username))
                {
                    ConfigCacheHelper.consolidatedWrapper.Users.Remove(username);
                }
                ConfigCacheHelper.consolidatedWrapper.Users.Add(username, user);
                ConfigCacheHelper.SaveConsolidatedFile();
            }
        }
Exemple #2
0
        private static User LoadUser(string username)
        {
            User user;

            if (ConfigCacheHelper.UseDiscreteFiles)
            {
                if (userCache.TryGetValue(username, out user))
                {
                    return(user);
                }
                lock (userCache)
                {
                    if (userCache.TryGetValue(username, out user))
                    {
                        return(user);
                    }
                    var fileName = ConfigCacheHelper.GetLocalFileName(username, "user");
                    if (File.Exists(fileName))
                    {
                        var fileContent = File.ReadAllText(fileName);
                        if (ConfigCacheHelper.EncryptFiles())
                        {
                            fileContent = fileContent.Decrypt(ConfigCacheHelper.LocalEncryptionKey);
                        }
                        user = JsonConvert.DeserializeObject <User>(fileContent);
                        userCache.TryAdd(username, user);
                        return(user);
                    }
                    user = GetConfiguration(username);
                    userCache.TryAdd(username, user);
                    var content = JsonConvert.SerializeObject(user);
                    if (ConfigCacheHelper.EncryptFiles())
                    {
                        content = content.Encrypt(ConfigCacheHelper.LocalEncryptionKey);
                    }
                    File.WriteAllText(fileName, content);
                    return(user);
                }
            }
            else
            {
                lock (userCache)
                {
                    ConfigCacheHelper.GetOrCreateConsolidatedFile();
                    if (ConfigCacheHelper.consolidatedWrapper.Users.TryGetValue(username, out user))
                    {
                        return(user);
                    }
                    user = GetConfiguration(username);
                    ConfigCacheHelper.consolidatedWrapper.Users.Add(username, user);
                    ConfigCacheHelper.SaveConsolidatedFile();
                    return(user);
                }
            }
        }