I am trying to create a new Sitecore 9.0.1 XP cache and add an item to it. I can create the cache just fine. When I call the Add method it executes just fine but nothing is added to the cache.

I have a local variable call viewCache

private readonly ICache<string> viewCache;

I have tried to do both of this.

   this.viewCache = Sitecore.Caching.CacheManager.GetNamedInstance<string>(

    // Also tried this instead of the above line. NOT both at the same time
   this.viewCache = new Cache("viewCache", 10);

Both execute just fine and create a cache object.

Later on I make this call.

this.viewCache.Add(cacheKey, returnViewpath);

Again it executes just fine. However, when I look at the cache the count is always 0 and I can't get the value out of the cache as it says it is null.

enter image description here


I think you just need ICache not ICache<string>

Also, Make sure your memory size is right. You want to use the string sizer, not an int. So, like StringUtil.ParseSizeString("10MB")

Here's an example of some heavily redacted code. Idea here is this shows you how I setup a Cache Service that could be exposed by dependency injection:

public class SampleCacheService : ISampleCacheService
    private readonly SampleCacheSettings _cacheSettings;
    private readonly ICache _SampleCache;

    public SampleCacheService(SampleCacheSettings cacheSettings)
        _cacheSettings = cacheSettings;
        _SampleCache = CacheManager.GetNamedInstance(_cacheSettings.Name, StringUtil.ParseSizeString(_cacheSettings.MaxSize), true);

    public SampleCacheSettings Settings => _cacheSettings;

    public void Clear()

    public void Refresh()
        catch (Exception ex)
            Log.Error($"[Sample Cache Error] Message: {ex.Message}", this);
            Log.Error($"[Sample Cache Error] Stack: {ex.StackTrace}", this);

    public void Refresh(string contentPath)
        Refresh(null, contentPath);

    public void Refresh(Item[] itemArray, string contentPath = "")
        //Fetch API
        var Sample = _someApiService.GetAsync(npiList);

        if (Sample == null) return;

        //Iterate List Add To Cache
        foreach (var sample in Sample.data.entities)
            if (Contains(sample.id))

            Add(sample.id, sample);

    public bool Contains(string key)
        return _SampleCache.ContainsKey(key);

    public Entity Get(string key)
        if (!Contains(key))

        return (Entity)_SampleCache.GetValue(key);

    private void Add(string key)
        var Sample = _someApiService.GetAsync(new List<string>(){key});

        if (Sample?.data?.entities == null || !Sample.data.entities.Any()) return;

        if (Contains(key))

        _SampleCache.Add(key, Sample.data.entities.FirstOrDefault());

    private void Add(string key, Entity rating)
        _SampleCache.Add(key, rating);

  • Right. I only listed both calls to show that I have tried it both ways, not both at the same time though. – ToddB Nov 15 '18 at 1:36
  • I think you just need ICache not ICache<string> – Pete Navarra Nov 15 '18 at 1:39
  • 2
    Also, Make sure your memory size is right. You want to use the string sizer, not an int. So, like StringUtil.ParseSizeString("10MB") – Pete Navarra Nov 15 '18 at 1:41
  • 1
    Yep, that was it. Needed to use StringUtil.ParseSizeString("10MB") and that fixed it. I kept ICahce<string> and it still works so it seems the key is setting the cache size as you noted and not with an int. Thanks! – ToddB Nov 15 '18 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.