本文整理汇总了C#中MemcachedClient.Set方法的典型用法代码示例。如果您正苦于以下问题:C# MemcachedClient.Set方法的具体用法?C# MemcachedClient.Set怎么用?C# MemcachedClient.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemcachedClient
的用法示例。
在下文中一共展示了MemcachedClient.Set方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadQueryInMemCached
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
*/
//.........这里部分代码省略.........