sqlalchemy / sqlalchemy/dogpile.cache

feature idea - cache callbacks

Open
#75 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature low priority
Dominant language
Python
Stars
299
Forks
50
PR merge metrics
No merged PRs in 30d

Description

Migrated issue, originally created by jvanasco (jvanasco)

before trying to build this out, I wanted to propose these extensions to CacheRegion.get_or_create and CacheRegion.cache_on_arguments:

  • callback_get_fn
  • callback_create_fn

If these values are set, before returning the value from the cache it will be passed into the appropriate function as a filter (or a notification).

if GET and callback_get_fn:
       value = callback_get_fn(value)
elif CREATE and callback_create_fn:
       value = callback_create_fn(value)
return value

In some ways this is similar to a proxy, however this pattern would give a lot more flexibility when the cache generating functions have 'byproducts' of setting up miscellaneous data

Consider this example:

def expensive_function(self):
      self.foo = 'bar'
      return (1, 2)

def outer(self, args):
        @cache_on_arguments
        def inner():
              (a, b) = self.expensive_function()
              return (a, b)
        (a, b) = inner(args)

on the first run, self.foo is set; on the second run it is not.

when migrating functions to use dogpile, a lot of issues like this creep up. While the proper route is to avoid this pattern, that can take a lot of rewriting. If there was a mechanism such as a callback that could be used to derive that the value is cached -- then it would be relatively simple to write a few lines of code to ensure parity with whatever environment an uncached data generation would have created.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading CacheRegion.get_or_create and CacheRegion.cache_on_arguments, the two entry points named in the issue. Trace their cache-hit and value-creation paths, then determine how callback_get_fn and callback_create_fn should behave and verify that callbacks provide the requested state-restoration behavior without changing normal return values.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.