Memcache   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了linq-to-sql – 无论如何都要为Memcached serilize linq对象?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始切换到memcached,目前正在使用memcached进行测试.

我有2个对象,我创建了一个对象并在其上放置了[serializable](例如,让我们调用这个Object1),另一个对象是使用Linq DBML(Object2)创建的.

我试着memcached List< Object1>,它工作得很好,就像魅力一样,这里的一切都是缓存并正确加载.

但接着,我转到Linq对象,现在我尝试添加到memcached List< Object2>这不起作用,它根本没有添加到memcached.没有添加密钥

我继续并将序列化模式更改为单向,再次添加,仍然没有希望.

反正有没有让这项工作?

这是我刚刚编写的简单测试,使用COdeplex中的MemcachedProvider来演示:

public ActionResult test()
{
    var returnObj = distCache.Get<List<Post>>("testKey");
    if (returnObj == null)
    {
        DataContext _db = new DataContext();
        returnObj = _db.Posts.ToList();
        distCache.Add("testKey",returnObj,new TimeSpan(29,0));
        _db.dispose();
    }

    return Content(returnObj.First().titlE);
}

这是来自Memcached,没有调用STORE:

> NOT FOUND _x_testKey
>532 END
<528 get _x_testKey
> NOT FOUND _x_testKey
>528 END
<516 get _x_testKey
> NOT FOUND _x_testKey
>516 END

在我的sql分析器中,它调用3个查询3个测试时间=>证明从Memcached回调的对象为null,然后进行查询.

解决方法

看起来默认实现(DefaultTranscoder)是使用BinaryFormatter; “单向”东西是对不同序列化器(DataContractserializer)的指令,并且不添加[serializable].

(注意:我已经添加了一个@L_301_0@来尝试为memcached编写一个protobuf-net转码器;这很酷并且可以免费修复大部分内容)

我没有测试过,但有几个选项可供选择:

>编写一个不同的代码转换器实现,检测[DataContract]并使用DataContractserializer,并挂钩此代码转换器
>通过部分类将[serializable]添加到您的类型中(由于LINQ字段类型不可序列化,我不相信这会起作用)
>在使用DataContractserializer的分部类中添加Iserializable实现
>喜欢3,但使用protobuf-net,其中a使用“单向”,而b:比DataContractserializer更快更小
>编写可序列化的DTO并将您的类型映射到该DTO

最后一个很简单,但可能会增加更多的工作.

我很想先看第3个选项,因为第1个选项涉及重建提供者;第四个选项也肯定会列在我要测试的东西列表中.

由于DCS在反序列化期间返回了不同的对象,我在3中挣扎;我转而使用protobuf-net,所以这里有一个版本,它显示了为现有的[DataContract]类型添加一个部分类,使其可以与BinaryFormatter一起使用.实际上,我怀疑(有证据)这也会使它效率高(比原始[serializable]):

using System;
using System.IO;
using System.Runtime.serialization;
using System.Runtime.serialization.Formatters.binary;
using ProtoBuf;

/* DBML generated */
namespace My.object.Model
{
    [DataContract]
    public partial class myType
    {
        [DataMember(Order = 1)]
        public int id { get; set; }

        [DataMember(Order = 2)]
        public String Name { get; set; }
    }
}
/* Your extra class file */
namespace My.object.Model
{
    // this adds **extra** codE into the exisTing myType
    [serializable]   
    public partial class myType : Iserializable {
        public myType() {}
        void Iserializable.GetobjectData(serializationInfo info,StreamingContext context) {
            serializer.serialize(info,this);
        }
        protected myType(serializationInfo info,StreamingContext context) {
            serializer.Merge(info,this);
        }
    }
}
/* quick test via BinaryFormatter */
namespace My.App
{
    using My.object.Model;
    static class Program
    {
        static void Main()
        {
            BinaryFormatter bf = new BinaryFormatter();
            myType obj = new myType { Id = 123,Name = "abc" },clone;
            using (MemoryStream ms = new MemoryStream())
            {
                bf.serialize(ms,obj);
                ms.Position = 0;
                clone = (myTypE)bf.Deserialize(ms);
            }
            Console.WriteLine(clone.Id);
            Console.WriteLine(clone.Name);
        }
    }
}

大佬总结

以上是大佬教程为你收集整理的linq-to-sql – 无论如何都要为Memcached serilize linq对象?全部内容,希望文章能够帮你解决linq-to-sql – 无论如何都要为Memcached serilize linq对象?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:现在