Skip to content

Consider scoped caching mechanism #504

Description

@Yaraslaut

While it is easy to use BelongsTo relations, it might introduce to many reloading of the same entries from the database, for instance

struct A
{
  int pk;
  std::string something;
};

struct B
{
  int pk;
  BelongsTo<&A::pk> a
};

and then we want to loop over vector<B> and do something with the connected A

for(const auto& b: vector_of_B)
  b.a->something; // will trigger lazy loading of the A record

now imagine that in the database we have only few records in A, but thousands in B, we should provide a way to load only once the records from the A table.

Proposal:

Implement scoping caching

struct Logic
{
  DataMapperCaching _cache; // data member that is holding cache of loaded records
  void foo();
};


Logic::foo()
{
  DataMapperCaching cache {_cache}; // _cache is the data member and DataMapperCaching populates missing entries on destructor
  for(const auto& b: vector_of_B)
    b.a->something; // will trigger lazy loading of the A record if it does not exist in the cache
}  

We should figure out what is the good type erasure mechanism here and also, maybe in some use cases you want to restrict caching only for one table, for instance DataMapperCaching<A> should cache only A records

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions