Base solution for your next web application
Open Closed

Entity Cache #6927


User avatar
0
antonis created

Hi,

I am looking to store Entities in Cache instead of querying the database all the time and when an entity is CRUD these changes to be also replicated to Cache.

I read Caching in aspnetboilerplate documentation but it does not support the above requirement and it is also not storing Entitis but rather Dtos. Is there anything that support my requirements?

Thanks in advance


13 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    There may be many problems with storage entities, such as Entities may depend on each other. Entities may have a lot of state or proxy information attached to different Orm(ef6).

    So abp is designed to cache the Dto of the entity instead of the entity itself.

    Dto can add all the entity information you need.

  • User Avatar
    0
    antonis created

    So every time a create/update/delete an entity from the database the Cache dto is updated?

    Thanks

  • User Avatar
    0
    maliming created
    Support Team
    • It gets the entity from the repository (the database) in it's first call. It then gets from the cache in subsequent calls.
    • It automatically invalidates a cached entity if this entity is updated or deleted. Thus, it will be retrieved from the database in the next call.

    please see:https://aspnetboilerplate.com/Pages/Documents/Caching#how-entitycache-works

  • User Avatar
    0
    antonis created

    It lacks one very important thing though. No way to retrieve all objects at once (list) but instead I can only retrieve it one by one. This is very inefficient especially in cases that you want to return to client an OutputDto which contains list of items for drawing controls (e.g Countries dropdown need to get all countries)

  • User Avatar
    0
    maliming created
    Support Team

    If your entity does not change often, you can directly store the Dto of the list in the cache, and re-query the list Dto update cache when some entities change.

    If your entity changes frequently, you can consider adding/deleting/updating the cached list Dto when certain entities are changed. Of course this is cumbersome.

    https://aspnetboilerplate.com/Pages/Documents/Caching https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events#entity-changes

  • User Avatar
    0
    antonis created

    I am confused.

    It automatically invalidates a cached entity if this entity is updated or deleted. Thus, it will be retrieved from the database in the next call.

    and then you said that:

    If your entity changes frequently, you can consider adding/deleting/updating the cached list Dto when certain entities are changed. Of course this is cumbersome.

    Is it creating/updating/deleting or I have to do it manually

    If your entity does not change often, you can directly store the Dto of the list in the cache, and re-query the list Dto update cache when some entities change.

    How can I do that?

  • User Avatar
    0
    maliming created
    Support Team

    I mean you don't use Entity Caching,

    Instead, use the cache component and entity change events to handle it yourself.

    Please refer to the documentation: https://aspnetboilerplate.com/Pages/Documents/Caching https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events#entity-changes

  • User Avatar
    0
    antonis created

    Eventually I have to write my own cache. I think this functionality is very easy to be added to existing ITypeCache since it is very easy to implement it.

  • User Avatar
    0
    antonis created

    What do you mean?

  • User Avatar
    0
    maliming created
    Support Team

    Sorry,

    Getting all the keys from the cache seems to have not been implemented.

    Please see: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2842

  • User Avatar
    0
    hikalkan created
    Support Team

    @antonis,

    ABP's cache abstraction is distributed cache compatible. That means we can not add methods supported only by the memory cache.

    You have a few options explained by @maliming.

    You can free to implement this yourself, using with an in-memory cache or a simple static list/dictionary of entities. This won't work if you use a clustered deployment (becuase inmemory cache will not be shared between servers).

  • User Avatar
    0
    antonis created

    Yes I totally understand this. Mostly I intend to use it for entries that are not created/updated/deleted

  • User Avatar
    0
    ismcagdas created
    Support Team

    Closing then :)