Object caching seems doesn't work on the DXC

Vote:
 

Hi guys,

Our customer are using hosting service of DXC. Recently, we have the issue regarding the output cache. Some objects that we added to mem cache seem always return directly (not from cache). Do you have any idea for this issue?

Here are my code to implement object cache :

public interface ICache
	{
		T Get(string key);
		void Add(T objectToCache, string key, TimeSpan offset);
		void Remove(string key);
		bool Exists(string key);
		void Clear();
	}

	public class EPiServerCache : ICache
	{
		private readonly string[] _cacheDependencyKeys;
		private readonly ISynchronizedObjectInstanceCache _objectInstanceCache;

		public EPiServerCache(ISynchronizedObjectInstanceCache objectInstanceCache, IContentCacheKeyCreator cacheKeyCreator)
		{
			_cacheDependencyKeys = new[] { cacheKeyCreator.VersionKey };
			_objectInstanceCache = objectInstanceCache;
		}

		public T Get(string key)
		{
			T cacheItem;

			try
			{
				cacheItem = (T)_objectInstanceCache.Get(key);
			}
			catch (Exception)
			{
				cacheItem = default(T);
			}

			return cacheItem;
		}


		public void Add(T objectToCache, string key, TimeSpan expiration)
		{
			var evictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute, _cacheDependencyKeys);
			_objectInstanceCache.Insert(key, objectToCache, evictionPolicy);
		}

		public bool Exists(string key)
		{
			return (_objectInstanceCache.Get(key) != null);
		}

		public void Clear()
		{
			_objectInstanceCache.Clear();
		}

		public void Remove(string key)
		{
			_objectInstanceCache.Remove(key);
		}
	}
#184551
Oct 31, 2017 9:17
Vote:
 

Hi Quan,

If you cache the object depend on IContentCacheKeyCreator.VersionKey, then you will have to make sure that the entry exists in the cache first before inserting the record, otherwise the object will not be cached. The entry for IContentCacheKeyCreator.VersionKey will be removed if a content is updated on a remote server, so you'll need to call IContentCacheVersion.Version to ensure the entry is present. Could you please try to change your code to this and keep us updated of the result:

public class EPiServerCache : ICache
{
    private readonly string[] _cacheDependencyKeys;
    private readonly ISynchronizedObjectInstanceCache _objectInstanceCache;
    private readonly IContentCacheVersion _contentCacheVersion;

    public EPiServerCache(ISynchronizedObjectInstanceCache objectInstanceCache, IContentCacheKeyCreator cacheKeyCreator, IContentCacheVersion contentCacheVersion)
    {
        _cacheDependencyKeys = new[] { cacheKeyCreator.VersionKey };
        _objectInstanceCache = objectInstanceCache;
        _contentCacheVersion = contentCacheVersion;
    }

    public T Get<T>(string key)
    {
        T cacheItem;

        try
        {
            cacheItem = (T)_objectInstanceCache.Get(key);
        }
        catch (Exception)
        {
            cacheItem = default(T);
        }

        return cacheItem;
    }


    public void Add<T>(T objectToCache, string key, TimeSpan expiration)
    {
        // IContentCacheKeyCreator.VersionKey must exists in the cache. Otherwise 
        // the entries are not cached (the key is removed when a remote server content is updated).
        // Version call ensures that the key is present
        var version = _contentCacheVersion.Version;

        var evictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute, _cacheDependencyKeys);
        _objectInstanceCache.Insert(key, objectToCache, evictionPolicy);
    }

    public bool Exists(string key)
    {
        return (_objectInstanceCache.Get(key) != null);
    }

    public void Clear()
    {
        _objectInstanceCache.Clear();
    }

    public void Remove(string key)
    {
        _objectInstanceCache.Remove(key);
    }
}

Thanks,

Linh Doan

#188137
Feb 13, 2018 7:52
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.