Enyim Memcached client doesn't work with expir

2019-01-28 07:54发布

when using Enyim memcached client to store data with a expire timespan, i find that it doesn't work. Could anyone help?

In my test code, I store the date with 10 minutes expiration time, and i try to get it from cache immediately but got a null object.

Enyim.Caching.MemcachedClient client = new Enyim.Caching.MemcachedClient();
client.Store(Enyim.Caching.Memcached.StoreMode.Set, "one", 1, new TimeSpan(0, 10, 0));

object obj;
client.TryGet("one", out obj); // obj == null

Assert.AreNotEqual(null, obj); // this will fail

6条回答
乱世女痞
2楼-- · 2019-01-28 08:21

I'm not sure if you're being vague w/ your example or if you really are using an object type but I had the same problem using custom objects. Integers, strings, etc. would work fine but my custom object was always NULL as soon as I put it in the cache. Turns out I didn't have the Serializable attribute on my object. Once I put that on there everything worked as expected.

查看更多
聊天终结者
3楼-- · 2019-01-28 08:23

Create a memcached client object is a costly operation, so try to create it on start of the application and reuse that object.

This how I initiate MemcachedClient Object and access Membese Build using Enyim client.

public class MemcachedManager
{
    protected static MemcachedClient mc = null;
    public MemcachedManager()
    {
        if (mc == null) initMemCached();
    }

    private void initMemCached()
    {
        MemcachedClientConfiguration config = new MemcachedClientConfiguration();
        string ipstr = "192.168.1.1;192.168.1.2"; // List of Memcaced nodes
        string[] ips = ipstr.Split(';');
        foreach (string ip in ips)
        {
            config.Servers.Add(new IPEndPoint(IPAddress.Parse(ip), 11211));
        }
        config.Protocol = MemcachedProtocol.Binary;
        mc = new MemcachedClient(config);
    }

    public void setWithTimeOut(string key, string value, int minutes)
    {
        mc.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value, TimeSpan.FromMinutes(minutes));
    }

    public string getString(string key)
    {
        return (string)mc.Get(key);
    }

    public void setString(string key, string value)
    {
        mc.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value);
    }

}
查看更多
放我归山
4楼-- · 2019-01-28 08:29

The answer is that memcached(windows) 1.4.4 64-bit version has this bug. If it fits your needs you can try 1.4.4 32-bit version for windows, or you can try to find another 64-bit compiled version. This problem also took my whole day and I finally installed "1.4.4 32-bit version" and viola, everything works perfect with 1.4.4. 32-bit.

查看更多
来,给爷笑一个
5楼-- · 2019-01-28 08:32

Please check your server.

1.Use MemCacheD Manager

Or

2.Use telnet 127.0.0.1 11211(change to your sever setting)

and type:stats

It would show your the stats.

See the "time" item stat.

It's second format you must convert it,

Simply you can compare with "1262517674",

If smaller than "1262517674",your memcached server it too old.

Please Upgrade Or change your memcached version.

Otherwise Just change your memcached version

查看更多
Ridiculous、
6楼-- · 2019-01-28 08:34

Hyacinthus is pretty precise on his question, the answers are somewhat irrelevant. I am having the same problem ONLY when setting an expiration, either as a Timespan or as a DateTime.

There is also an issue for this at Enyim's github https://github.com/enyim/EnyimMemcached/issues/110

We have a tweaked version at my company's codebase, but it is outdated. I will try to locate the fix and get back to this, and send a pull request to Enyim, when I find the time.

EDIT: I also found this on github.

I can confirm that this was not happening with other build of the memcached server. I think it's a bug with this particular build: Build with this issue: http://www.urielkatz.com/archive/detail/memcached-64-bit-windows Build WITHOUT this issue: http://blog.elijaa.org/index.php?post/2010/08/25/Memcached-1.4.5-for-Windows

Care to check the server version you are using? My initial comment still stands, as I run tests using the two different dlls, and the tweaked one works while the one shipped with CouchBase fails

查看更多
爷、活的狠高调
7楼-- · 2019-01-28 08:40

you need to get a sington first.

eg:

using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            MemcachedClient client = MemcachedManager.Instance;//new MemcachedClient();

            client.Store(StoreMode.Set, "b", "bb", new TimeSpan(1, 1, 300));
            var x = client.Get("b");

            client.Store(StoreMode.Set, "a", "aa");
            var y = client.Get("a");

            client.Store(StoreMode.Set, "c", "cc", DateTime.Now.AddSeconds(300));
            var z = client.Get("c");

            client.Store(StoreMode.Set, "c", "ccc");
            var t = client.Get("c");
            Console.WriteLine("{0},{1},{2}",x,y,z);
        }
    }

    public static class MemcachedManager
    {
        private readonly static MemcachedClient _instance;

        static MemcachedManager()
        {
            _instance = new MemcachedClient();
        }

        public static MemcachedClient Instance { get { return _instance; } }
    }
}
查看更多
登录 后发表回答