一、阿里云OCS简单介绍

  阿里云OCS兼容Memcached,因为OCS就相当于Memcached的服务器端,我们代码只是当作客户端,链接上服务器端就行了。阿里云OCS介绍详情见 http://www.aliyun.com/product/ocs?spm=5176.2020520107.0.0.s2zgFk#Help 。

二、C#客户端链接OCS

  1.阿里云文档上介绍的是用Enyim.Caching去链接OCS。那我们项目中就用nuget去下载Enyim.Caching包。nuget搜索Enyim,搜索到的结果如下图,第一个是nolog版本的,第二个是带log的,随便一个都可以。

  nuget搜索结果如下:

  C#链接阿里云OCS-LMLPHP

  2.安装成功以后,再链接OCS,写个初始化memcached的代码,代码如下。

 using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks; namespace OCS
{
public class MemCached
{
private static MemcachedClient MemClient; static readonly object padlock = new object();
//线程安全的单例模式
public static MemcachedClient getInstance(string hostName,string userName,string password)
{
if (MemClient == null)
{
lock (padlock)
{
if (MemClient == null)
{
MemClientInit(hostName,userName,password);
}
}
}
return MemClient;
} static void MemClientInit(string hostName,string userName,string password)
{
try
{
//初始化缓存
MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
IPAddress newaddress = IPAddress.Parse(Dns.GetHostEntry(hostName).AddressList[].ToString());
IPEndPoint ipEndPoint = new IPEndPoint(newaddress, ); // 配置文件 - ip
memConfig.Servers.Add(ipEndPoint);
// 配置文件 - 协议
memConfig.Protocol = MemcachedProtocol.Binary;
// 配置文件-权限
memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
memConfig.Authentication.Parameters["zone"] = "";
memConfig.Authentication.Parameters["userName"] = userName;
memConfig.Authentication.Parameters["password"] = password;
//下面请根据实例的最大连接数进行设置
memConfig.SocketPool.MinPoolSize = ;
memConfig.SocketPool.MaxPoolSize = ;
MemClient = new MemcachedClient(memConfig);
}
catch (Exception)
{
MemClient = null;
}
}
}
}

  3.再写个服务去调用上面的初始化代码,创建MemcachedClient的实例去调用客户端代码。下面代码中的构造函数中的_setting.HostAddress, _setting.AccessId, _setting.AccessKey分别对应的是,OCS的链接地址,链接密码和OCS的实例ID。

 using Enyim.Caching;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using Zupo.Core.Caching;
using ServiceStack.Text;
using Newtonsoft.Json; namespace OCS
{
public class OCSService
{
IOCSSetting _setting; private MemcachedClient client; public OCSService(IOCSSetting setting)
{
this._setting = setting;
this.client = MemCached.getInstance(_setting.HostAddress, _setting.AccessId, _setting.AccessKey);
} /// <summary>
/// 是否链接上服务器
/// </summary>
protected bool LinkServer
{
get
{
return client == null ? false : true;
}
} protected IGetOperationResult ExecuteGet(string key)
{
return client.ExecuteGet(key);
} /// <summary>
/// 根据key获得缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected object Get(string key)
{
return client.Get(key);
} /// <summary>
/// 根据key获得缓存 泛型方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
protected T Get<T>(string key)
{
try
{
//JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
//string objectStr = client.Get(key).ToString();
var obj = client.Get(key);
if (obj != null)
{
return JsonConvert.DeserializeObject<T>(obj.ToString());
}
return default(T);
//return objectStr.FromJson<T>();
}
catch (Exception)
{
this.Remove(key);
return default(T);
}
} /// <summary>
/// 根据key获得缓存 泛型方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
protected List<T> GetList<T>(string key)
{
var objectStr = client.Get(key).ToString(); //return objectStr.FromJson<List<T>>();
return JsonConvert.DeserializeObject<List<T>>(objectStr);
} /// <summary>
/// 根据数组key获得数据 IDictionary
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
protected IDictionary<string, object> GetByKeys(string[] keys)
{
return client.Get(keys);
} /// <summary>
/// 该key值缓存是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Contains(string key)
{
object value; return client.TryGet(key, out value);
} /// <summary>
/// 存储,有的话直接覆盖
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
protected bool Set(string key, object value)
{
return client.Store(StoreMode.Set, key, value);
} /// <summary>
/// 存储,有的话直接覆盖
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
protected bool Set<T>(string key, T value)
{
try
{
//执行序列化
//string objectStr = value.ToJson();
string objectStr = JsonConvert.SerializeObject(value, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
return client.Store(StoreMode.Set, key, objectStr);
}
catch(Exception)
{
return false;
} } /// <summary>
/// 存储,有的话直接覆盖
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
protected bool Set<T>(string key, List<T> value)
{
//执行序列化
//string objectStr = value.ToJson();
string objectStr = JsonConvert.SerializeObject(value, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
return client.Store(StoreMode.Set, key, objectStr);
} /// <summary>
/// 存储,有的话直接覆盖
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expire">失效时间:TimeSpan</param>
/// <returns></returns>
protected bool SetExpires(string key, object value, TimeSpan expire)
{
try
{
//string objectStr = value.ToJson();
string objectStr = JsonConvert.SerializeObject(value, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
return client.Store(StoreMode.Set, key, objectStr, expire);
}
catch(Exception)
{
return false;
} } /// <summary>
/// 存储,有的话直接覆盖
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expire">失效时间:TimeSpan</param>
/// <returns></returns>
protected bool SetExpires<T>(string key, T value, TimeSpan expire)
{
//执行序列化
//string objectStr = value.ToJson();
string objectStr = JsonConvert.SerializeObject(value, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
return client.Store(StoreMode.Set, key, objectStr, expire);
} /// <summary>
/// 删除cached
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public void Remove(string key)
{
client.Remove(key);
} /// <summary>
/// Removes items by pattern
/// 该方法未实现
/// </summary>
/// <param name="pattern">pattern</param>
public virtual void RemoveByPattern(string pattern)
{
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
//var keysToRemove = new List<String>(); //var allkeys = client.
//foreach (var key in allkeys)
// if (regex.IsMatch(key))
// keysToRemove.Add(key); //foreach (string key in keysToRemove)
//{
// Remove(key);
//}
} /// <summary>
/// 清空ocs缓存
/// </summary>
/// <returns></returns>
public void Clear()
{
client.FlushAll();
}
}
}

MemcachedClient

三、使用注意点

  因为OCS只支持key/value的格式进行存储,所以我项目中先运用了Newtonsoft.Json去先对对象序列化,再存入到OCS上,读取的时候再反序列化读取。代码如下:

 /// <summary>
/// 存储,有的话直接覆盖
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
protected bool Set<T>(string key, T value)
{
try
{
//执行序列化
string objectStr = JsonConvert.SerializeObject(value, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
return client.Store(StoreMode.Set, key, objectStr);
}
catch(Exception)
{
return false;
} }

代码中设置了JsonSerializerSettings,因为项目用的是EF,一些对象里面的属性可能是另外一个对象还有是一些死循环的属性,所以数据量会比较大,直接执行序列化会内存溢出的,所以要在对象里面不需要序列化的属性上加上[JsonIgnore],忽略此属性或字段序列化。
那么反序列化获取数据代码如下:

 /// <summary>
/// 根据key获得缓存 泛型方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
protected T Get<T>(string key)
{
try
{
var obj = client.Get(key);
if (obj != null)
{
return JsonConvert.DeserializeObject<T>(obj.ToString());
}
return default(T);
}
catch (Exception)
{
this.Remove(key);
return default(T);
}
}
05-04 01:00