sqlalchemy / sqlalchemy/dogpile.cache

I would like my cached methods to never return expired values and block wait instead.

Open
#264 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello,

I have a multiproces/thread scenario where my cached function can be called at the same time.

I end up falling into the scenario of
https://github.com/sqlalchemy/dogpile.cache/blob/de85b101f864bd0f5f3ba97dc96950b94f143d82/dogpile/cache/region.py#L954-L958

So what I have is an expired value in the cache.
Thread 1 retrieves it, notices it's expired, starts creating a new value, and will return the new value.
Meanwhile; Thread 2 retrieves it, notices it's expired, notices thread 1 is busy creating and returns the old value.

But the old value is entirely useless for my case. It's expired for a reason. Is there a way to configure the cache region to never return expired values and just block/wait for creation instead. Just like a cache miss?

(simplified) Code to reproduce my issue:

import os
import time
from multiprocessing import Process

from dogpile.cache import make_region

cache_region = make_region()
cache_region.configure(
    "dogpile.cache.dbm",
    expiration_time=1,
    arguments={
        "filename": "/tmp/cache.dbm",
    },
)


@cache_region.cache_on_arguments()
def get_value():
    print(os.getpid(), "Making new value...")
    time.sleep(2)
    return f"Got value {time.time():.0f}"


def run():
    print(os.getpid(), get_value())


print("3 sync calls in a row (value should be made once):")
run()
run()
run()

processes = []
for i in range(2):
    process = Process(target=run)
    processes.append(process)

time.sleep(2)
print()
print("2 async calls after cache expired (value should be made once) and be different from the previous:")
for process in processes:
    process.start()
for process in processes:
    process.join()

Example output:

3 sync calls in a row (value should be made once):
103954 Making new value...
103954 Got value 1740579012
103954 Got value 1740579012
103954 Got value 1740579012

2 async calls after cache expired (value should be made once) and be different from the previous:
103978 Got value 1740579012
103977 Making new value...
103977 Got value 1740579016

In the above output, the line 103978 Got value 1740579012 is bad for me. That should never return an expired value.

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 with dogpile/cache/region.py at the linked lines 954-958 and trace how an expired value is handled while another process creates a replacement. Use the supplied multiprocessing reproduction to verify the current behavior. Done means concurrent callers wait for the refreshed value instead of receiving the expired one, with the reproduction showing both calls return the new value.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.