0

We wish to make use of Sitecores customcache. However, we are in doubt whether we need to implement ICacheable or whether its enough to make use of the abstract CustomCache class. This is for Sitecore 9.2.

public class ShopCache : CustomCache, IShopCache
{
    public ShopCache(long maxSize) : base("ShopCache", maxSize)
    {
    }

    public T Get<T>(string cacheKey)
    {
        return (T)this.GetObject(cacheKey);
    }

    public void Set<T>(string cacheKey, T obj)
    {
        SetObject(cacheKey, obj);
    }
}

public interface IShopCache
{
    T Get<T>(string cacheKey);

    void Set<T>(string cacheKey, T obj);
}

Here is an example of an object we would like to cache:

public class AccountDataDto
{
    public AccountItemDto[] Accounts { get; set; }

    public LinksItemDto[] Links { get; set; }
}

public class AccountItemDto
{
    public string Number { get; set; }

    public string Address { get; set; }

    public string Name  { get; set; }

    public DateTime Date { get; set; }

    public int NumberOfTransactions { get; set; }

    public AccountBalanceDto OpeningBalance { get; set; }

    public AccountBalanceDto ClosingBalance { get; set; }

    public AccountTransactionDto[] Transactions { get; set; }

    public AccountPeriodDto[] Periods { get; set; }

    public AccountItemDto()
    {

    }
}

public class AccountBalanceDto
{
    public DateTime Date { get; set; }
    public string Label { get; set; }
    public decimal Amount { get; set; }
    public string Currency { get; set; }

    public AccountBalanceDto()
    {

    }
}

public class AccountPeriodDto
{
    public string StartDate { get; set; }
    public string EndDate { get; set; } 
    public decimal Amount { get; set; }
    public string Currency { get; set; } 

    public AccountPeriodDto()
    {

    }
}

public class AccountTransactionDto
{
    public DateTime Date { get; set; }
    public string Description { get; set; }
    public decimal Amount { get; set; }
    public string Currency { get; set; } 

    public AccountTransactionDto()
    {

    }
}

It seems that we are able to use both add and retrieve objects from the custom cache just fine, without having to implement ICacheable, but I am unsure, if I should implement it anyhow? Or is it mainly used by older Sitecore versions?

1 Answer 1

1

Implementing the ICacheable interface provides you with more flexibility when it comes to handling complex objects and your caching strategy. It allows you to define a size calculation inside the GetDataLength method and avoid expensive size calculation in strategy

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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