Show More
@@ -1,120 +1,120 b'' | |||
|
1 | 1 | # Copyright (C) 2015-2023 RhodeCode GmbH |
|
2 | 2 | # |
|
3 | 3 | # This program is free software: you can redistribute it and/or modify |
|
4 | 4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
5 | 5 | # (only), as published by the Free Software Foundation. |
|
6 | 6 | # |
|
7 | 7 | # This program is distributed in the hope that it will be useful, |
|
8 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 | 10 | # GNU General Public License for more details. |
|
11 | 11 | # |
|
12 | 12 | # You should have received a copy of the GNU Affero General Public License |
|
13 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
14 | 14 | # |
|
15 | 15 | # This program is dual-licensed. If you wish to learn more about the |
|
16 | 16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
17 | 17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
18 | 18 | |
|
19 | 19 | import logging |
|
20 | 20 | import threading |
|
21 | 21 | |
|
22 | 22 | from dogpile.cache import register_backend |
|
23 | 23 | |
|
24 | 24 | from . import region_meta |
|
25 | 25 | from .utils import ( |
|
26 | 26 | ActiveRegionCache, |
|
27 | 27 | FreshRegionCache, |
|
28 | 28 | InvalidationContext, |
|
29 | 29 | backend_key_generator, |
|
30 | 30 | clear_cache_namespace, |
|
31 | 31 | get_default_cache_settings, |
|
32 | 32 | get_or_create_region, |
|
33 | 33 | make_region, |
|
34 | 34 | str2bool, |
|
35 | 35 | ) |
|
36 | 36 | |
|
37 | 37 | module_name = 'rhodecode' |
|
38 | 38 | |
|
39 | 39 | register_backend( |
|
40 | 40 | "dogpile.cache.rc.memory_lru", f"{module_name}.lib.rc_cache.backends", |
|
41 | 41 | "LRUMemoryBackend") |
|
42 | 42 | |
|
43 | 43 | register_backend( |
|
44 | 44 | "dogpile.cache.rc.file_namespace", f"{module_name}.lib.rc_cache.backends", |
|
45 | 45 | "FileNamespaceBackend") |
|
46 | 46 | |
|
47 | 47 | register_backend( |
|
48 | 48 | "dogpile.cache.rc.redis", f"{module_name}.lib.rc_cache.backends", |
|
49 | 49 | "RedisPickleBackend") |
|
50 | 50 | |
|
51 | 51 | register_backend( |
|
52 | 52 | "dogpile.cache.rc.redis_msgpack", f"{module_name}.lib.rc_cache.backends", |
|
53 | 53 | "RedisMsgPackBackend") |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | log = logging.getLogger(__name__) |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | FILE_TREE_CACHE_VER = 'v5' |
|
60 | 60 | LICENSE_CACHE_VER = 'v3' |
|
61 | 61 | PERMISSIONS_CACHE_VER = 'v2' |
|
62 | 62 | |
|
63 | 63 | CLEAR_DELETE = 'delete' |
|
64 | 64 | CLEAR_INVALIDATE = 'invalidate' |
|
65 | 65 | |
|
66 | 66 | |
|
67 |
def async_creation_runner(cache, |
|
|
67 | def async_creation_runner(cache, cache_key, creator, mutex): | |
|
68 | 68 | |
|
69 | 69 | def runner(): |
|
70 | 70 | try: |
|
71 | 71 | value = creator() |
|
72 |
cache.set( |
|
|
72 | cache.set(cache_key, value) | |
|
73 | 73 | finally: |
|
74 | 74 | mutex.release() |
|
75 | 75 | |
|
76 | 76 | thread = threading.Thread(target=runner) |
|
77 | 77 | thread.start() |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def configure_dogpile_cache(settings): |
|
81 | 81 | cache_dir = settings.get('cache_dir') |
|
82 | 82 | if cache_dir: |
|
83 | 83 | region_meta.dogpile_config_defaults['cache_dir'] = cache_dir |
|
84 | 84 | |
|
85 | 85 | rc_cache_data = get_default_cache_settings(settings, prefixes=['rc_cache.']) |
|
86 | 86 | |
|
87 | 87 | # inspect available namespaces |
|
88 | 88 | avail_regions = set() |
|
89 | 89 | for key in rc_cache_data.keys(): |
|
90 | 90 | namespace_name = key.split('.', 1)[0] |
|
91 | 91 | if namespace_name in avail_regions: |
|
92 | 92 | continue |
|
93 | 93 | |
|
94 | 94 | avail_regions.add(namespace_name) |
|
95 | 95 | log.debug('dogpile: found following cache regions: %s', namespace_name) |
|
96 | 96 | |
|
97 | 97 | new_region = make_region( |
|
98 | 98 | name=namespace_name, |
|
99 | 99 | function_key_generator=None, |
|
100 | 100 | async_creation_runner=None |
|
101 | 101 | ) |
|
102 | 102 | |
|
103 | 103 | new_region.configure_from_config(settings, f'rc_cache.{namespace_name}.') |
|
104 | 104 | new_region.function_key_generator = backend_key_generator(new_region.actual_backend) |
|
105 | 105 | |
|
106 | 106 | async_creator = str2bool(settings.pop(f'rc_cache.{namespace_name}.async_creator', 'false')) |
|
107 | 107 | if async_creator: |
|
108 | 108 | log.debug('configuring region %s with async creator', new_region) |
|
109 | 109 | new_region.async_creation_runner = async_creation_runner |
|
110 | 110 | |
|
111 | 111 | if log.isEnabledFor(logging.DEBUG): |
|
112 | 112 | region_args = dict(backend=new_region.actual_backend, |
|
113 | 113 | region_invalidator=new_region.region_invalidator.__class__) |
|
114 | 114 | log.debug('dogpile: registering a new region key=`%s` args=%s', namespace_name, region_args) |
|
115 | 115 | |
|
116 | 116 | region_meta.dogpile_cache_regions[namespace_name] = new_region |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | def includeme(config): |
|
120 | 120 | configure_dogpile_cache(config.registry.settings) |
General Comments 0
You need to be logged in to leave comments.
Login now