Have problem while getting data from Memcached on .NET MVC solution.
I have this custom repository:
public List<DropDownLocalization> GetLocalization(string key, string lang)
{
var result = cacheClient.Get<IQueryable<DropDownLocalization>>("DD_" + key + "_" + lang);
if (result == null)
{
int getLangId = _db.languages.Where(d => d.Association == lang).Select(d => d.Id).FirstOrDefault();
int getLableItemId = _db.lables_dropdown.Where(d => d.Key == key).Select(d => d.Id).FirstOrDefault();
var get = _db.lables_dropdown_items.Where(d => d.LableId == getLableItemId).Select(d => new DropDownLocalization
{
DDId = d.Id,
DDName = d.lables_dropdown_values.Where(m => m.Language == getLangId).Select(m => m.Value).FirstOrDefault()
}).AsQueryable();
cacheClient.Store(StoreMode.Add, "DD_" + key + "_" + lang, (IQueryable<DropDownLocalization>)get);
EFQueryLogger.WriteQuery(((ObjectQuery)get).ToTraceString());
return get.ToList();
}
return result.ToList();
}
It's get list of drop down labels localized from current lang.
So it's trying to get data from cache, if not success put data to cache. While i comment part of code with "if" statement (just to see if it's get data data from cache) i have error of null reference. It's mean that request response is not in the cache.
Can somebody put my nose to the problem?
There is my enym client library config:
<enyim.com>
<memcached protocol="Text">
<servers>
<add address="localhost" port="11211" />
</servers>
<socketPool deadTimeout="00:00:10" />
</memcached>
</enyim.com>
Solution found. Unfortunately it was my mistakes. There is the practice how to store data model in memcached.
I have model:
[Serializable]
public class DropDownLocalization
{
public int DDId { get; set; }
public string DDName { get; set; }
}
And method that return result:
public List<DropDownLocalization> GetLocalization(string key, string lang)
{
List<DropDownLocalization> result = MemcachedSingleton.Instance.Get<List<DropDownLocalization>>("DD_" + key + "_" + lang);
if (result == null)
{
int getLangId = _db.languages.Where(d => d.Association == lang).Select(d => d.Id).FirstOrDefault();
int getLableItemId = _db.lables_dropdown.Where(d => d.Key == key).Select(d => d.Id).FirstOrDefault();
result = _db.lables_dropdown_items.Where(d => d.LableId == getLableItemId).Select(d => new DropDownLocalization
{
DDId = d.Id,
DDName = d.lables_dropdown_values.Where(m => m.Language == getLangId).Select(m => m.Value).FirstOrDefault()
}).ToList();
MemcachedSingleton.Instance.Store(StoreMode.Add, "DD_" + key + "_" + lang, result);
return result;
}
return result;
}
It's fully working now.
Config should be next:
<enyim.com>
<memcached protocol="Text">
<servers>
<add address="localhost" port="11211" />
</servers>
<transcoder type="Enyim.Caching.Memcached.DataContractTranscoder, Enyim.Caching" />
<socketPool deadTimeout="00:00:10" />
</memcached>
</enyim.com>