c# - MEMCACHED get 方法总是返回 null

标签 c# asp.net .net memcached

我正在尝试使用 Memcached 在我们的应用程序中实现缓存,我能够将键和值设置到服务器,但是当我尝试获取值时,它总是返回 null。下面是我的示例代码片段。

//memcached configuration in my web config
<enyim.com>
  <memcached protocol="Binary">
    <servers>
      <add address="127.0.0.1" port="11211"/>
    </servers>
   <socketPool minPoolSize="10" maxPoolSize="100" 
                  connectionTimeout="00:10:00" deadTimeout="00:05:00"/>

  </memcached>
</enyim.com>
<configSections>
    <sectionGroup name="enyim.com">
        <section name="memcached"
           type="Enyim.Caching.Configuration.MemcachedClientSection,
           Enyim.Caching"/>
    </sectionGroup>
</configSections>





 //Get method in my controller
 public object GetSalesOrder()
 {
      using (Enyim.Caching.MemcachedClient mc = new Enyim.Caching.MemcachedClient())
      {
          mc.FlushAll();
          var salesOrders = salesOrderListService.GetSalesOrders();
          byte[] val;
          val = S.Serializer.objectToByteArray(salesOrders);
          mc.Store(Enyim.Caching.Memcached.StoreMode.Set, "salesOrderList", val);
          byte[] data = mc.Get<byte[]>("salesOrderList");
          var returnObj = S.Serializer.ByteArrayToObject<List<Model.SalesOrderList>>((byte[])val);

          return returnObj;
       }            
  }


//Model

[Serializable]
[ResourceType("SalesOrderList")]
public class SalesOrderList
{
    public int Id { get; set; }
    public string Code{ get; set; }
    public string CustomerName{ get; set; }
    public DateTime OrderDate { get; set; }
    public DateTime ShipDate { get; set; }
}

//Serializer class
public class Serializer
    {
        public static byte[] objectToByteArray<T>(T obj)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                formatter.Serialize(ms, obj);
                return ms.ToArray();
            }
        }

        public static Object ByteArrayToObject<T>(byte[] arrBytes)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                ms.Write(arrBytes, 0, arrBytes.Length);
                ms.Seek(0, System.IO.SeekOrigin.Begin);

                object obj = (Object)formatter.Deserialize(ms);

                return obj;
            }
        }
    }

我错过了什么吗?

最佳答案

我在您的代码中看到的一件事是您没有用于存储或检索数据的缓存键。我在我的代码中这样做的方式如下:

// create cachekey for storing and retrieving
var cacheKey = Guid.NewGuid().ToString();
// store for an hour
// memcachedClient.Store( StoreMode.Set, SanitizeCacheKey( cacheKey ), value, validFor )
mc.Store(StoreMode.Set, cacheKey , val, new TimeSpan(0, 1, 0, 0));

AccessCache.Get<byte[]>( cacheKey )

关于c# - MEMCACHED get 方法总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36168976/

相关文章:

asp.net - 使用 AspNetZero 单点登录

c# - 图像对应用程序内存使用的影响

c# - 如何比较类型

c# - 计算今天是否是纽约证券交易所的交易日的例行程序?

c# - ViewModel 不包含电子邮件的定义

javascript - asp.net mvc javascript 禁用显示和隐藏 div

c# - System.ArgumentException 值不在预期范围内,SQL 问题

c# - 使用通用类型的 EntityFramework 的 CRUD 操作

javascript - ASP.NET WebMethod 将整个页面返回给 JQuery ajax 请求

asp.net - telerik asp.net 控制清除客户端