protected override void OnStop() { /* * Stop the timer and dispose objects */ //dispose configuration object this.Configuration = null; //stop and dispose the refresh timer MemcachedLoaderService.RefreshTimer.Enabled = false; MemcachedLoaderService.RefreshTimer.Dispose(); MemcachedLoaderService.RefreshTimer = null; }
public static bool ReloadRedisServer(MemcachedLoaderConfig Configuration) { bool Refreshed = false; string ErrMsg = string.Empty; if (Configuration != null && Configuration.CachedQueriesCollection != null && Configuration.CachedQueriesCollection.Count > 0) { foreach (CachedQuery CacheQuery in Configuration.CachedQueriesCollection) { if (!LoadQueryInRedis(Configuration, CacheQuery, out ErrMsg)) { Utils.GetEventLog().WriteEntry(string.Format("MemcachedLoaderService.ReloadRedisServer. Error: {0}.", ErrMsg)); } } } /* * Return refresh results */ return Refreshed; }
public static MemcachedLoaderConfig LoadConfiguration(string XmlDocPath) { MemcachedLoaderConfig config = new MemcachedLoaderConfig(); try { XmlDocument XmlDoc = new XmlDocument(); XmlDoc.Load(XmlDocPath); /* * Load cache service refresh/reload entire cache seconds internal. Service will reload all queries based on this interval */ XmlNode RefreshSeconds = XmlDoc.SelectSingleNode("/configuration/reload_entire_cache_seconds"); config.ReloadEntireCacheSeconds = int.Parse(RefreshSeconds.InnerText); /* * Enable Redis Caching Flag */ XmlNode EnableRedisCaching = XmlDoc.SelectSingleNode("/configuration/enable_redis_caching"); config.EnableRedisCaching = bool.Parse(EnableRedisCaching.InnerText); /* * Enable Memcached Caching */ XmlNode EnableMemcachedCaching = XmlDoc.SelectSingleNode("/configuration/enable_memcached_caching"); config.EnableMemcachedCaching = bool.Parse(EnableMemcachedCaching.InnerText); /* * Load memcached server connections settings */ MemcachedSettings MemcachedServerSettings = new MemcachedSettings(); //server string mcServer = XmlDoc.SelectSingleNode("/configuration/memcached/server").InnerText; MemcachedServerSettings.Server = mcServer; //port int mcPort = int.Parse(XmlDoc.SelectSingleNode("/configuration/memcached/port").InnerText); MemcachedServerSettings.Port = mcPort; //cache items pin seconds int mcCacheItemExpireSeconds = int.Parse(XmlDoc.SelectSingleNode("/configuration/memcached/cache_object_seconds").InnerText); MemcachedServerSettings.CacheObjectSeconds = mcCacheItemExpireSeconds; /* * Load Redis server connection settings */ RedisSettings RedisServerSettings = new RedisSettings(); //redis server host string redisServer = XmlDoc.SelectSingleNode("/configuration/redis/server").InnerText; RedisServerSettings.Server = redisServer; //redis server port int redisPort = int.Parse(XmlDoc.SelectSingleNode("/configuration/redis/port").InnerText); RedisServerSettings.Port = redisPort; //redis server password string redisPassword = XmlDoc.SelectSingleNode("/configuration/redis/password").InnerText; RedisServerSettings.Password = redisPassword; //redis global cache object seconds setting int redisCacheItemExpireSeconds = int.Parse(XmlDoc.SelectSingleNode("/configuration/redis/cache_object_seconds").InnerText); RedisServerSettings.CacheObjectSeconds = redisCacheItemExpireSeconds; /* * Load MySQL database connection settings - for now a single server support */ DatabaseSettings MySqlConfig = new DatabaseSettings(); //server string dbType = XmlDoc.SelectSingleNode("/configuration/database_settings/db_type").InnerText; MySqlConfig.DBType = dbType; //server string dbServer = XmlDoc.SelectSingleNode("/configuration/database_settings/server").InnerText; MySqlConfig.Server = dbServer; //port string dbPort = XmlDoc.SelectSingleNode("/configuration/database_settings/port").InnerText; MySqlConfig.Port = dbPort; //username string dbUsername = XmlDoc.SelectSingleNode("/configuration/database_settings/username").InnerText; MySqlConfig.Username = dbUsername; //password string dbPassword = XmlDoc.SelectSingleNode("/configuration/database_settings/password").InnerText; MySqlConfig.Password = dbPassword; //password string dbName = XmlDoc.SelectSingleNode("/configuration/database_settings/database").InnerText; MySqlConfig.Database = dbName; /* * Load all objects in main configuration object */ config.MemcachedConnectionSettings = MemcachedServerSettings; config.RedisConnectionSettings = RedisServerSettings; config.DBConnectionSettings = MySqlConfig; config.CachedQueriesCollection = LoadQueriesSettings(XmlDoc.SelectNodes("/configuration/cache_queries/query")); } catch (Exception ex) { EventLog eventLog = Utils.GetEventLog(); string ErrorMessage = string.Format("MemcachedLoaderService. Error loading Service Configuration XML File. Error message was [{0}].", ex.Message); eventLog.WriteEntry(ErrorMessage); eventLog.Dispose(); throw new ApplicationException(ErrorMessage); } /* * Returns fresh instance of configuration settings */ return config; }
private void LoadConfiguration() { /* * Load XML configuration file */ string ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "ServiceConfiguration.xml"; this.Configuration = MemcachedLoaderConfig.LoadConfiguration(ConfigPath); }
public static bool LoadQueryInRedis(MemcachedLoaderConfig Config, CachedQuery QueryToLoad, out string ErrMsg) { bool LoadedQuery = false; ErrMsg = string.Empty; Dictionary<string, Dictionary<string, string>> MemoryDict; IRedisClientsManager RedisClientManager; string RedisConnectionString = Config.RedisConnectionSettings.GetConnectionString(); try { using (RedisClientManager = new PooledRedisClientManager(RedisConnectionString)) { /* * Get Redis Client */ var client = RedisClientManager.GetClient(); /* * Retrieve Query Data from MySql */ DataTable QueryDataTable = Utils.GetDataTable(Config.DBConnectionSettings, QueryToLoad); /* * Determine whether to permanently persist kvp cached object in Redis */ bool PersistCachedObject = (Config.RedisConnectionSettings.CacheObjectSeconds <= 0); /* * Cache each row from the data table as a JSON serialized dictionary into the Redis Cache Server */ if (QueryDataTable != null && QueryDataTable.Rows.Count > 0) { //Define a dictionary to store the data table to be serialized into a JSON object MemoryDict = null; ErrMsg = string.Empty; /* * Convert DataTable / MySQL Query ResultSet in Dictionary<string,Dictionary<string,string>> object */ bool Success = Utils.GetQueryCacheDictionaryFromDataTable(Config.DBConnectionSettings, QueryToLoad, QueryDataTable, out MemoryDict, out ErrMsg); /* * Table Data Dictionary was successfully created - Cached each row in Memcached as a JSON dictionary */ if (Success) { foreach (KeyValuePair<string, Dictionary<string, string>> TableDictionaryKvp in MemoryDict) { string Key = TableDictionaryKvp.Key; string JsonStoreValue = JsonConvert.SerializeObject(TableDictionaryKvp.Value, new KeyValuePairConverter()); /* * Store value permanently or set an Expires Datetime */ if (PersistCachedObject) { LoadedQuery = client.Set<string>(Key, JsonStoreValue); } else { LoadedQuery = client.Set<string>(Key, JsonStoreValue, DateTime.Now.AddSeconds(Config.RedisConnectionSettings.CacheObjectSeconds)); } } } } /* * Successfully loaded data table in Redis Cache */ LoadedQuery = true; Utils.GetEventLog().WriteEntry(string.Format("[MemcachedLoaderService.Redis] Successfully loaded table [{0}] in the memory cache.", QueryToLoad.KeyPrefix)); } } catch (Exception ex) { LoadedQuery = false; ErrMsg = string.Format("[MemcachedLoaderService.Redis] Can't load query into Cache. Memcached Error Message [{0}].", ex.Message); Utils.GetEventLog().WriteEntry(ErrMsg); } return LoadedQuery; }
public static bool LoadQueryInMemCached(MemcachedLoaderConfig Config, CachedQuery QueryToLoad, out string ErrorMessage) { bool LoadedQuery = false; ErrorMessage = string.Empty; ResponseCode response = ResponseCode.UnknownCommand; Dictionary<string, Dictionary<string, string>> MemoryDict; try { /* * Connect to memcached server */ ServerConnectionCollection MemCachedServers = new ServerConnectionCollection(); /* * Add Server from Config Settings */ MemCachedServers.Add(Config.MemcachedConnectionSettings.Server, port: Config.MemcachedConnectionSettings.Port); /* * Create the client */ IConnectionProvider provider = new ConnectionProvider(MemCachedServers); MemcachedClient client = new MemcachedClient(provider); /* * Retrieve Query Data from MySql */ DataTable QueryDataTable = GetDataTable(Config.DBConnectionSettings, QueryToLoad); /* * Determine whether to permanently persist kvp cached object in Redis */ bool PersistCachedObject = (Config.MemcachedConnectionSettings.CacheObjectSeconds <= 0); /* * Cache each row from the data table as a JSON serialized dictionary */ if (QueryDataTable != null && QueryDataTable.Rows.Count > 0) { //Define a dictionary to store the data table to be serialized into a JSON object MemoryDict = null; string ErrMsg = string.Empty; /* * Convert DataTable / MySQL Query ResultSet in Dictionary<string,Dictionary<string,string>> object */ bool Success = Utils.GetQueryCacheDictionaryFromDataTable(Config.DBConnectionSettings, QueryToLoad, QueryDataTable, out MemoryDict, out ErrMsg); /* * Table Data Dictionary was successfully created - Cached each row in Memcached as a JSON dictionary */ if (Success) { foreach (KeyValuePair<string, Dictionary<string, string>> TableDictionaryKvp in MemoryDict) { string Key = TableDictionaryKvp.Key; string JsonStoreValue = JsonConvert.SerializeObject(TableDictionaryKvp.Value, new KeyValuePairConverter()); /* * Determine right expiration Datetime value */ DateTime ExpireDate = (PersistCachedObject) ? DateTime.MaxValue : DateTime.Now.AddSeconds(Config.MemcachedConnectionSettings.CacheObjectSeconds); /* * Load Kvp in Memcached */ response = client.Set(Key, JsonStoreValue, ExpireDate); /* * If key already exists replace it */ if (response == ResponseCode.KeyExists) { response = client.Replace(Key, JsonStoreValue, ExpireDate); } } } } /* * Success */ LoadedQuery = (response == ResponseCode.NoError); Utils.GetEventLog().WriteEntry(string.Format("[MemcachedLoaderService.Memcached] Successfully loaded table [{0}] in the memory cache.", QueryToLoad.KeyPrefix)); } catch (Exception ex) { ErrorMessage = string.Format("[MemcachedLoaderService.Memcached] Can't load query into Cache. Memcached Error Message [{0}].", ex.Message); Utils.GetEventLog().WriteEntry(ErrorMessage); } /* * Results */ return LoadedQuery; }