Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit b2b971f

Browse files
authored
Enable cache time-based expiry by default (#11849)
1 parent 4d7e74b commit b2b971f

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

changelog.d/11849.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enable cache time-based expiry by default. The `expiry_time` config flag will be superseded by `expire_caches` and `cache_entry_ttl`.

docs/sample_config.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,16 @@ caches:
751751
per_cache_factors:
752752
#get_users_who_share_room_with_user: 2.0
753753

754-
# Controls how long an entry can be in a cache without having been
755-
# accessed before being evicted. Defaults to None, which means
756-
# entries are never evicted based on time.
754+
# Controls whether cache entries are evicted after a specified time
755+
# period. Defaults to true. Uncomment to disable this feature.
757756
#
758-
#expiry_time: 30m
757+
#expire_caches: false
758+
759+
# If expire_caches is enabled, this flag controls how long an entry can
760+
# be in a cache without having been accessed before being evicted.
761+
# Defaults to 30m. Uncomment to set a different time to live for cache entries.
762+
#
763+
#cache_entry_ttl: 30m
759764

760765
# Controls how long the results of a /sync request are cached for after
761766
# a successful response is returned. A higher duration can help clients with

docs/upgrade.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ to:
111111
112112
Please update any relevant reverse proxy or firewall configurations appropriately.
113113
114+
## Time-based cache expiry is now enabled by default
115+
116+
Formerly, entries in the cache were not evicted regardless of whether they were accessed after storing.
117+
This behavior has now changed. By default entries in the cache are now evicted after 30m of not being accessed.
118+
To change the default behavior, go to the `caches` section of the config and change the `expire_caches` and
119+
`cache_entry_ttl` flags as necessary. Please note that these flags replace the `expiry_time` flag in the config.
120+
114121
## Deprecation of `capability` `org.matrix.msc3283.*`
115122
116123
The `capabilities` of MSC3283 from the REST API `/_matrix/client/r0/capabilities`

synapse/config/background_updates.py

Whitespace-only changes.

synapse/config/cache.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
import os
1617
import re
1718
import threading
@@ -23,6 +24,8 @@
2324

2425
from ._base import Config, ConfigError
2526

27+
logger = logging.getLogger(__name__)
28+
2629
# The prefix for all cache factor-related environment variables
2730
_CACHE_PREFIX = "SYNAPSE_CACHE_FACTOR"
2831

@@ -148,11 +151,16 @@ def generate_config_section(self, **kwargs) -> str:
148151
per_cache_factors:
149152
#get_users_who_share_room_with_user: 2.0
150153
151-
# Controls how long an entry can be in a cache without having been
152-
# accessed before being evicted. Defaults to None, which means
153-
# entries are never evicted based on time.
154+
# Controls whether cache entries are evicted after a specified time
155+
# period. Defaults to true. Uncomment to disable this feature.
156+
#
157+
#expire_caches: false
158+
159+
# If expire_caches is enabled, this flag controls how long an entry can
160+
# be in a cache without having been accessed before being evicted.
161+
# Defaults to 30m. Uncomment to set a different time to live for cache entries.
154162
#
155-
#expiry_time: 30m
163+
#cache_entry_ttl: 30m
156164
157165
# Controls how long the results of a /sync request are cached for after
158166
# a successful response is returned. A higher duration can help clients with
@@ -217,12 +225,30 @@ def read_config(self, config, **kwargs) -> None:
217225
e.message # noqa: B306, DependencyException.message is a property
218226
)
219227

220-
expiry_time = cache_config.get("expiry_time")
221-
if expiry_time:
222-
self.expiry_time_msec: Optional[int] = self.parse_duration(expiry_time)
228+
expire_caches = cache_config.get("expire_caches", True)
229+
cache_entry_ttl = cache_config.get("cache_entry_ttl", "30m")
230+
231+
if expire_caches:
232+
self.expiry_time_msec: Optional[int] = self.parse_duration(cache_entry_ttl)
223233
else:
224234
self.expiry_time_msec = None
225235

236+
# Backwards compatibility support for the now-removed "expiry_time" config flag.
237+
expiry_time = cache_config.get("expiry_time")
238+
239+
if expiry_time and expire_caches:
240+
logger.warning(
241+
"You have set two incompatible options, expiry_time and expire_caches. Please only use the "
242+
"expire_caches and cache_entry_ttl options and delete the expiry_time option as it is "
243+
"deprecated."
244+
)
245+
if expiry_time:
246+
logger.warning(
247+
"Expiry_time is a deprecated option, please use the expire_caches and cache_entry_ttl options "
248+
"instead."
249+
)
250+
self.expiry_time_msec = self.parse_duration(expiry_time)
251+
226252
self.sync_response_cache_duration = self.parse_duration(
227253
cache_config.get("sync_response_cache_duration", 0)
228254
)

0 commit comments

Comments
 (0)