简体   繁体   English

亚马逊的AWS Elasticache-C#示例

[英]Amazon's AWS elasticache - c# example

Does anyone have example code that use C# to connect to a AWS elasticache (memcached) cluster? 是否有人拥有使用C#连接到AWS Elasticache(memcached)集群的示例代码?

I've found this example but it seems a bit old(the library is from 2010). 我找到了这个示例,但它似乎有点旧了(该库来自2010年)。 This github library may work. 这个github库可能有效。 There aren't many examples out there. 那里没有很多例子。

Thanks, 谢谢,

Got it working with this pluralsight video by Richard Seroter. 它与Richard Seroter的这个多元视角视频一起工作。

Steps: 脚步:

Add the following nuget package: EnyimMemCached 添加以下nuget包:EnyimMemCached

Then add this inside the configSections node in your web config: 然后将其添加到您的Web配置中的configSections节点

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

Then Add this just below the system.web node( so it's a sibling node to system.web). 然后将其添加到system.web节点的正下方 (因此它是system.web的同级节点)。 Be sure to replace the url and port with your elasticache endpoint : 确保用您的elasticache端点替换url和port:

<enyim.com>
    <memcached>
      <servers>
        <add address="...your elasticache url here...." port="your port here..."></add>
      </servers>
    </memcached>
  </enyim.com>

Then in my view action i called set a cache value and read it out. 然后在我的视图操作中,我调用了设置一个缓存值并读出来。 It only works when it's been published and is running on AWS.(wasn't working locally): 它仅在发布并在AWS上运行时才有效(不在本地运行):

 public ActionResult Index()
        {
            var client = new MemcachedClient();

            string myCacheKey = "MyCacheKey";
            client.Store(Enyim.Caching.Memcached.StoreMode.Set, myCacheKey, "If you see this it worked.");  // set the cache.
            string myCachedString = client.Get<string>(myCacheKey);

            ViewBag.MyCache = myCachedString ?? "**** SORRY,  DIDN'T WORK..***..";
            return View();

        }

Hop this helps someone. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM