2016-12-22 64 views
0

以下是我嘗試在控制檯應用程序中使用MemcacheMemcache - 緩存數據始終爲空

App.Config中

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="enyim.com"/> 
     <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection,Enyim.Caching"/> 
    </configSections> 

<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
</startup> 

<enyim.com> 
    <memcached protocol="Binary"> 
    <severs> 
     <add address="127.0.0.1" port="11211"/> 
    </severs> 

    </memcached> 
    </enyim.com> 
</configuration> 

C#

static void Main(string[] args) 
    { 
     DateTime dt = DateTime.Now; 
     try 
     { 

      using (MemcachedClient memcache = new MemcachedClient()) 
      { 
       string cachedTime = memcache.Get<string>("CachedTime"); 
       if (cachedTime == null) 
       { 
        memcache.Store(Enyim.Caching.Memcached.StoreMode.Set, "CachedTime", DateTime.Now.ToString()); 
       cachedTime = memcache.Get<string>("CachedTime"); 
       } 

       Console.WriteLine(cachedTime); 
       Console.WriteLine(dt); 
      } 
     } 
     catch (Exception ex) 
     { 

     } 
    } 

每次的cachedDate值爲NULL

回答

0

更改App.Config中下面的,它應該工作:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="enyim.com"> 
     <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> 
    </sectionGroup> 
    </configSections> 

    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 

    <enyim.com> 
    <memcached> 
     <servers> 
     <add address="127.0.0.1" port="11211" /> 
     </servers> 
     <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" /> 
    </memcached> 
    </enyim.com> 
</configuration>