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