public static void TestNMemcachedClient()
        {
            /*
             * Connect to memcached server
             */
            ServerConnectionCollection MemCachedServers = new ServerConnectionCollection();

            /*
             * Add Server from Config Settings
             */
            MemCachedServers.Add("192.168.1.48", 11211);

            /*
             * Create the client
             */
            IConnectionProvider provider = new ConnectionProvider(MemCachedServers);
            MemcachedClient client = new MemcachedClient(provider);

            System.Console.WriteLine("Dumping contents of cache...");
            System.Console.WriteLine("Printing California Customers table [calcustomers]");
            System.Console.WriteLine();

            ResponseCode response = ResponseCode.UnknownCommand;

            IDictionary<string, object> Responses = client.Get(new string[] { "calcustomers.key=1", "calcustomers.key=2", "calcustomers.key=3" });

            foreach (KeyValuePair<string, object> item in Responses)
            {
                System.Console.WriteLine("key: {0}, CustomerName: {1}", item.Key, item.Value.ToString());
            }

            System.Console.WriteLine("Press any key to exit....");
            System.Console.ReadLine();
        }
Beispiel #2
0
        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;
        }