• Memcache缓存


    memcache 是服务器缓存系统,以键值对方式保存数据到内存中,把对象序列化后,理论上可支持所有的数据类型。
    使用情景:怎么用都可以,注意的是它只把数据保存到内存中,重启memcache 服务后丢失,如果要持久化,须要额外程序处理。
    一般在web系统中用memcache 缓存常用的数据来缓解数据库查询压力和提高系统性能。它相当于数据库和程序间的中间件。
    memcache 早就如雷贯耳,想要用到系统中往往无从下手,下面就花一分钟时间把memcache 用起来;

    快速入门(quick start)

    服务器端配置

    • 就一个exe ,下载后用命令安装即可, 下载memcached
    • 以管理员身份方式运行 cmd 命令提示符;
    • cd 到下载memcached.exe 所在的路径;
    • 输入下面的安装命令,即可把 memcache 安装到windows 服务;
    • 打开windows服务,找到memcached server 服务,运行即可启动 memcached 服务,默认端口11211, 就用这端口,不用改;

      memcached.exe -d install
      命令安装

    安装后的服务

    客户端使用

    • NuGet安装EnyimMemcached

    • xml配置

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
      <!-- memcached 配置开始 -->
        <configSections>
      <sectionGroup name="enyim.com">
        <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
      </sectionGroup>
        </configSections>
        <enyim.com>
      <memcached>
        <servers>
          <add address="127.0.0.1" port="11211" />
        </servers>
        <transcoder type="ProtoBuf.Caching.Enyim.NetTranscoder, protobuf-net.Enyim" />
      </memcached>
        </enyim.com>
        <!-- memcached 配置结束 -->
        <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
      </configuration>

      看到了吧,上面的 <add address="127.0.0.1" port="11211" /> 就是memcache 服务器的 ip 和开放接口, 默认就是 11211

    • 开撸代码

    
        class Program
        {
            static void Main(string[] args)
            {
    
                MemcachedClient client = new MemcachedClient();
    
                //-- 新增或更新,存在key, 则覆盖
                client.ExecuteStore(StoreMode.Set, "test-set-key1", "whatever set 1");
                //-- 新增或更新,存在key, 则覆盖: 指定有效期 1 小时
                client.ExecuteStore(StoreMode.Set, "test-set-key2", "whatever set 2", DateTime.Now.AddHours(1));
    
    
                //-- 新增, 存在该key, 则不覆盖
                client.ExecuteStore(StoreMode.Add, "test-add-key1", "whatever add 1");
    
                //-- 更新, 不存在该key, 则不做更新
                client.ExecuteStore(StoreMode.Replace, "test-replace-key1", "whatever replace key1");
    
    
                //-- 取值
                client.Get<string>("test-set-key1"); // whatever set 1
                client.Get<string>("test-add-key1"); // whatever add 1
    
    
                //-- 缓存对象: 对象必须可序列化
    
                Foo foo = new Foo { Id = 1, Name = "foo1" };
                client.ExecuteStore(StoreMode.Set, "obj1", foo, DateTime.Now.AddHours(1));
    
                //取值
                var cacheFoo = client.Get<Foo>("obj1");
    
            }
        }
    
        [Serializable]
        public class Foo
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

    就这么简单

    下面深入点点

    配合数据库做缓存的处理流程

    为了缓存数据是最新的,必须要处理与数据库同步,有两种方式:

    • 同时更新数据库和缓存;
    • 只更新数据库,根据key, 把缓存移除或设为null,按上面的处理流程,下次拿到null, 就从数据库取最新的数据并缓存;

    如何维护key是非常关键的工作;

    请求 WebApi, 返回对象带有 k__BackingField

    不能用Serializable 修饰类,但缓存对象必须可序列化,可改用 protobuf-net 的 DataContract 修饰类, DataMember 修饰属性;
    虽然比较麻烦,但性能比.net 自带的 Serializable 要好;

    用了 protobuf-net 序列化对象后,有时 memcache 的 fGet<T> 范型方法莫名抛出异常,这是 protobuf-net 的bug, 最简便的方法是加try catch , 也可拿它源码改;

  • 相关阅读:
    脏数据
    LeetCode之Sort List
    COCOS2D-X FRAME动画创作随笔
    基于OpenCV性别识别
    Twitter 新一代流处理工具——Heron 该纸币Storm Limitations
    oracle设备
    苏州面对公司发布
    Weblogic监控指标
    mysql TIME_WAIT
    weblogic监控
  • 原文地址:https://www.cnblogs.com/aeexiaoqiang/p/6529416.html
Copyright © 2020-2023  润新知