문제

I am trying to fetch key value using Enyim client library from Memcache server.

After connecting to memcache server, I am able to write/replace key values but while using get operation following exception is being thrown:

NotSupportedException was unhandled Operation is not supported by the server or the request was malformed. If the latter please report the bug to the developers.

Please note, I am able to read this value using telnet/other memcache libraries.

I am not sure if I am missing something or is there bug in Enyim package?

Here is code.

using System;
using System.Net;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;

namespace Memcached
{
public class MemcacheClient
{
    private MemcachedClient _mc;
    private static readonly TimeSpan MinExpirationTimeSpan = new TimeSpan(0, 0, 30);


    public  MemcacheClient()
    {
        Startup();
    }

    private void Startup()
    {
        var config = new MemcachedClientConfiguration();
        config.Servers.Add(new IPEndPoint(IPAddress.Loopback, 11212));
       // config.Servers.Add(new IPEndPoint(IPAddress.Loopback, 11213));
        config.Protocol = MemcachedProtocol.Text;
        _mc = new MemcachedClient(config);
    }


    public bool SetItem(string key, object value)
    {
        if (_mc != null)
            return _mc.Store(StoreMode.Set, key, value);
        return false;
    }

    public bool SetItem(string key, object value, TimeSpan duration)
    {
        _ValidateExpirationDuration(duration);

        if (_mc != null)
        {
           var restult = _mc.Store(StoreMode.Set, key, value, DateTime.Now + duration);
            _mc.FlushAll();
            return restult;
        }
        return false;
    }

    private static void _ValidateExpirationDuration(TimeSpan duration)
    {
        if (duration <= MinExpirationTimeSpan)
            throw new ArgumentException("Cache expiration times of less than 30 seconds are ignored", "duration");
    }

    public object GetItem(string key)
    {
        if (_mc != null)
           return  _mc.Get(key); //<----------getting exception here
        return null;
    }

    public T GetItem<T>(string key) where T : class
    {
        var result = GetItem(key);
        if (result != null)
        {
            var targetObject = result as T;
            if (targetObject != null)
                return targetObject;
        }
        return null;
    }

    public bool Replace(string key, object value, TimeSpan duration)
    {
        _ValidateExpirationDuration(duration);
        if (_mc != null)
            return _mc.Store(StoreMode.Replace,  key, value, DateTime.Now + duration);
        return false;
    }

    public bool Replace(string key, object value)
    {
        if (_mc != null)
            return _mc.Store(StoreMode.Replace, key, value);
        return false;
    }


 }
}

Calling function

using System;
using System.Globalization;

namespace TryEnyim
{
class Program
{
    static void Main(string[] args)
    {
        var cache = new Memcached.MemcacheClient();
        Console.Write("Writing to cache: " + cache.SetItem("myString", "This is enyim @" + System.DateTime.Now.ToString(CultureInfo.InvariantCulture)));
        cache.Replace("myString", "This is enyim again @" + System.DateTime.Now.AddSeconds(10).ToString(CultureInfo.InvariantCulture));
        Console.WriteLine("Read key now: " + cache.GetItem<string>("myString"));// <----------getting exception here

        Console.ReadLine();

    }
 }

}

I am using Enyim package (version 2.12.0.0) from NuGet.

Any help will be useful.

Thanks

도움이 되었습니까?

해결책

Finally! I got solution.

After spending time on code debugging, I decided to upgrade Memcached server from 1.2.4 to 1.4.5.

After Memcached server upgrade, Enyim caching client works like charm.

Simple Solution:

Upgrade Memcached server with latest version.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top