问题
I'm going to use CacheManager for my .net project. The problem is that I can't find any examples of CacheManager.Memcached usage.
This is how I use it:
public class GlobalCache
{
private static ICacheManager<object> memcachedClient { get; set; }
private static readonly object locker = new object();
static GlobalCache()
{
if (memcachedClient == null)
{
lock (locker)
{
memcachedClient = CacheFactory.Build("memcached", settings => settings.WithMemcachedCacheHandle("memcached"));
}
}
}
}
Web.config:
<configuration>
<enyim.com>
<memcached protocol="Binary">
<servers>
<add address="127.0.0.1" port="11211" />
</servers>
</memcached>
</enyim.com>
<cache name="memcached">
<handle name="memcached"/>
</cache>
</configuration>
The error I have is: http://c2n.me/3hSHqvR.png - unknown section in web config.
If I remove all these sections, I have another runtime error: http://c2n.me/3hSI745.png - config error.
I tried to use settings.WithSystemRuntimeCacheHandle() instead of settings.WithMemcachedCacheHandle() and it works fine without any config sections. But in this case, my cache is cleared every time I restart my app. And what I want is - store cache in memcached storage, without any relation to my application.
So if you have some examples or small tutorial of how to use memcached with CacheManager - I will be much appreciated.
Thanks in advance!
回答1:
Regarding the error for the <cache name="memcached">
section , most likely you have not defined any section inside your configuration file with that name.
At least there is no section like that coming from CacheManager nor from enyim.
Then regarding the memcached handle. The name of the handle must match the section so that CacheManager can pick up the configuration.
The default is enyim.com/memcached
which you have. So you could either put enyim.com/memcached
or default
as the name of the memcached cache handle.
Like
CacheFactory.Build("memcached", settings => settings
.WithMemcachedCacheHandle("enyim.com/memcached"));
Let me know if this works for you.
I know this is not very well documented, yet. I might add something when I find some time ;)
来源:https://stackoverflow.com/questions/30308226/cachemanager-memcached-configuration