Show More
@@ -1,58 +1,63 b'' | |||||
1 | """SQLAlchemy Metadata and Session object""" |
|
1 | """SQLAlchemy Metadata and Session object""" | |
2 | from sqlalchemy.ext.declarative import declarative_base |
|
2 | from sqlalchemy.ext.declarative import declarative_base | |
3 | from sqlalchemy.orm import scoped_session, sessionmaker |
|
3 | from sqlalchemy.orm import scoped_session, sessionmaker | |
4 | from rhodecode.model import caching_query |
|
4 | from rhodecode.model import caching_query | |
5 | from beaker import cache |
|
5 | from beaker import cache | |
6 | import os |
|
6 | import os | |
7 | from os.path import join as jn, dirname as dn, abspath |
|
7 | from os.path import join as jn, dirname as dn, abspath | |
8 | import time |
|
8 | import time | |
9 |
|
9 | |||
10 | # Beaker CacheManager. A home base for cache configurations. |
|
10 | # Beaker CacheManager. A home base for cache configurations. | |
11 | cache_manager = cache.CacheManager() |
|
11 | cache_manager = cache.CacheManager() | |
12 |
|
12 | |||
13 | __all__ = ['Base', 'Session'] |
|
13 | __all__ = ['Base', 'Session'] | |
14 | # |
|
14 | # | |
15 | # SQLAlchemy session manager. Updated by model.init_model() |
|
15 | # SQLAlchemy session manager. Updated by model.init_model() | |
16 | # |
|
16 | # | |
17 | Session = scoped_session( |
|
17 | Session = scoped_session( | |
18 | sessionmaker( |
|
18 | sessionmaker( | |
19 | query_cls=caching_query.query_callable(cache_manager) |
|
19 | query_cls=caching_query.query_callable(cache_manager) | |
20 | ) |
|
20 | ) | |
21 | ) |
|
21 | ) | |
22 |
|
22 | |||
23 | # The declarative Base |
|
23 | # The declarative Base | |
24 | Base = declarative_base() |
|
24 | Base = declarative_base() | |
25 | #For another db... |
|
25 | #For another db... | |
26 | #Base2 = declarative_base() |
|
26 | #Base2 = declarative_base() | |
27 |
|
27 | |||
28 | #=============================================================================== |
|
28 | #=============================================================================== | |
29 | # CACHE OPTIONS |
|
29 | # CACHE OPTIONS | |
30 | #=============================================================================== |
|
30 | #=============================================================================== | |
31 |
cache_ |
|
31 | cache_base = jn(dn(dn(dn(abspath(__file__)))), 'data') | |
|
32 | cache_dir = jn(cache_base, 'cache') | |||
|
33 | ||||
|
34 | if not os.path.isdir(cache_base): | |||
|
35 | os.mkdir(cache_base) | |||
|
36 | ||||
32 | if not os.path.isdir(cache_dir): |
|
37 | if not os.path.isdir(cache_dir): | |
33 | os.mkdir(cache_dir) |
|
38 | os.mkdir(cache_dir) | |
34 | # set start_time to current time |
|
39 | # set start_time to current time | |
35 | # to re-cache everything |
|
40 | # to re-cache everything | |
36 | # upon application startup |
|
41 | # upon application startup | |
37 | start_time = time.time() |
|
42 | start_time = time.time() | |
38 | # configure the "sqlalchemy" cache region. |
|
43 | # configure the "sqlalchemy" cache region. | |
39 | cache_manager.regions['sql_cache_short'] = { |
|
44 | cache_manager.regions['sql_cache_short'] = { | |
40 | 'type':'memory', |
|
45 | 'type':'memory', | |
41 | 'data_dir':cache_dir, |
|
46 | 'data_dir':cache_dir, | |
42 | 'expire':10, |
|
47 | 'expire':10, | |
43 | 'start_time':start_time |
|
48 | 'start_time':start_time | |
44 | } |
|
49 | } | |
45 | cache_manager.regions['sql_cache_med'] = { |
|
50 | cache_manager.regions['sql_cache_med'] = { | |
46 | 'type':'memory', |
|
51 | 'type':'memory', | |
47 | 'data_dir':cache_dir, |
|
52 | 'data_dir':cache_dir, | |
48 | 'expire':360, |
|
53 | 'expire':360, | |
49 | 'start_time':start_time |
|
54 | 'start_time':start_time | |
50 | } |
|
55 | } | |
51 | cache_manager.regions['sql_cache_long'] = { |
|
56 | cache_manager.regions['sql_cache_long'] = { | |
52 | 'type':'file', |
|
57 | 'type':'file', | |
53 | 'data_dir':cache_dir, |
|
58 | 'data_dir':cache_dir, | |
54 | 'expire':3600, |
|
59 | 'expire':3600, | |
55 | 'start_time':start_time |
|
60 | 'start_time':start_time | |
56 | } |
|
61 | } | |
57 | #to use cache use this in query |
|
62 | #to use cache use this in query | |
58 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
|
63 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
General Comments 0
You need to be logged in to leave comments.
Login now