3

当使用 Enyim memcached 客户端存储具有过期时间跨度的数据时,我发现它不起作用。有人可以帮忙吗?

在我的测试代码中,我将日期存储为 10 分钟到期时间,我尝试立即从缓存中获取它,但得到了一个空对象。

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
4

6 回答 6

3

我不确定您是否对示例含糊不清,或者您是否真的在使用对象类型,但我在使用自定义对象时遇到了同样的问题。整数、字符串等可以正常工作,但我的自定义对象在我将其放入缓存后始终为 NULL。原来我的对象没有 Serializable 属性。一旦我把它放在那里,一切都按预期工作。

于 2011-12-08T19:39:28.353 回答
2

风信子对他的问题非常准确,答案有些无关紧要。只有在设置过期时间时,我才会遇到同样的问题,无论是作为时间跨度还是作为日期时间。

在 Enyim 的 github https://github.com/enyim/EnyimMemcached/issues/110上也有一个问题

我们在我公司的代码库中有一个经过调整的版本,但它已经过时了。当我找到时间时,我将尝试找到修复并返回此问题,并向 Enyim 发送拉取请求。

编辑:我也在github 上找到了这个。

我可以确认其他版本的 memcached 服务器没有发生这种情况。我认为这是这个特定构建的一个错误:构建这个问题: http ://www.urielkatz.com/archive/detail/memcached-64-bit-windows 构建没有这个问题: http ://blog.elijaa.org /index.php?post/2010/08/25/Memcached-1.4.5-for-Windows

关心检查您正在使用的服务器版本吗?我最初的评论仍然有效,因为我使用两个不同的 dll 运行测试,并且调整后的一个可以工作,而 CouchBase 附带的那个失败

于 2012-10-03T16:19:01.657 回答
2

请检查您的服务器。

1.使用 MemCacheD管理器

或者

2.使用telnet 127.0.0.1 11211(更改为您的服务器设置)

和类型:统计

它会显示你的统计数据。

请参阅“时间”项目统计信息。

这是您必须转换的第二种格式,

简单地你可以与“1262517674”进行比较,

如果小于“1262517674”,你的 memcached 服务器太旧了。

请升级或更改您的 memcached 版本。

否则只需更改您的 memcached 版本

于 2012-10-06T08:59:00.660 回答
1

答案是memcached(windows) 1.4.4 64位版本有这个bug。如果它符合您的需要,您可以尝试 1.4.4 32 位 windows 版本,或者您可以尝试找到另一个 64 位编译版本。这个问题也花了我一整天的时间,我终于安装了“1.4.4 32 位版本”和 viola,一切都与 1.4.4 完美配合。32 位。

于 2017-06-16T08:37:08.153 回答
0

你需要先得到一个sington。

例如:

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; } }
    }
}
于 2013-10-14T08:00:01.437 回答
0

创建 memcached 客户端对象是一项代价高昂的操作,因此请尝试在应用程序启动时创建它并重用该对象。

这就是我启动 MemcachedClient 对象并使用 Enyim 客户端访问 Membese Build 的方式。

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);
    }

}
于 2011-07-28T08:09:27.040 回答