開箱即用 - Memcache緩存


廢話少說,先上代碼C# memcache Demo

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 , 也可拿它源碼改;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM