mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 13:03:29 +00:00
per-request caches: Add per_request_cache library.
We have historically cached two types of values
on a per-request basis inside of memory:
* linkifiers
* display recipients
Both of these caches were hand-written, and they
both actually cache values that are also in memcached,
so the per-request cache essentially only saves us
from a few memcached hits.
I think the linkifier per-request cache is a necessary
evil. It's an important part of message rendering, and
it's not super easy to structure the code to just get
a single value up front and pass it down the stack.
I'm not so sure we even need the display recipient
per-request cache any more, as we are generally pretty
smart now about hydrating recipient data in terms of
how the code is organized. But I haven't done thorough
research on that hypotheseis.
Fortunately, it's not rocket science to just write
a glorified memoize decorator and tie it into key
places in the code:
* middleware
* tests (e.g. asserting db counts)
* queue processors
That's what I did in this commit.
This commit definitely reduces the amount of code
to maintain. I think it also gets us closer to
possibly phasing out this whole technique, but that
effort is beyond the scope of this PR. We could
add some instrumentation to the decorator to see
how often we get a non-trivial number of saved
round trips to memcached.
Note that when we flush linkifiers, we just use
a big hammer and flush the entire per-request
cache for linkifiers, since there is only ever
one realm in the cache.
This commit is contained in:
@@ -240,10 +240,10 @@ We generally try to avoid in-process backend caching in Zulip's Django
|
||||
codebase, because every Zulip production installation involves
|
||||
multiple servers. We do have a few, however:
|
||||
|
||||
- `per_request_display_recipient_cache`: A cache flushed at the start
|
||||
of every request; this simplifies correctly implementing our goal of
|
||||
not repeatedly fetching the "display recipient" (e.g. stream name)
|
||||
for each message in the `GET /messages` codebase.
|
||||
- `@return_same_value_during_entire_request`: We use this decorator to
|
||||
cache values in memory during the lifetime of a request. We use this
|
||||
for linkifiers and display recipients. The middleware knows how to
|
||||
flush the relevant in-memory caches at the start of a request.
|
||||
- Caches of various data, like the `SourceMap` object, that are
|
||||
expensive to construct, not needed for most requests, and don't
|
||||
change once a Zulip server has been deployed in production.
|
||||
|
||||
Reference in New Issue
Block a user