Mercurial > kallithea
annotate rhodecode/model/meta.py @ 559:bc4633a41967
make rhodecode reuse current session when not running on celery
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 07 Oct 2010 22:01:51 +0200 |
parents | 342f8434ed14 |
children | 0a48c1ec04fc c1c1cf772337 |
rev | line source |
---|---|
0 | 1 """SQLAlchemy Metadata and Session object""" |
2 from sqlalchemy.ext.declarative import declarative_base | |
3 from sqlalchemy.orm import scoped_session, sessionmaker | |
547
1e757ac98988
renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
482
diff
changeset
|
4 from rhodecode.model import caching_query |
482
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
5 from beaker import cache |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
6 import os |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
7 from os.path import join as jn, dirname as dn, abspath |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
8 import time |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
9 |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
10 # Beaker CacheManager. A home base for cache configurations. |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
11 cache_manager = cache.CacheManager() |
0 | 12 |
13 __all__ = ['Base', 'Session'] | |
14 # | |
15 # SQLAlchemy session manager. Updated by model.init_model() | |
16 # | |
482
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
17 Session = scoped_session( |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
18 sessionmaker( |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
19 query_cls=caching_query.query_callable(cache_manager) |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
20 ) |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
21 ) |
0 | 22 |
23 # The declarative Base | |
24 Base = declarative_base() | |
25 #For another db... | |
26 #Base2 = declarative_base() | |
482
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
27 |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
28 #=============================================================================== |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
29 # CACHE OPTIONS |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
30 #=============================================================================== |
550
342f8434ed14
fixed creation of cache data dir, on first run
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
31 cache_base = jn(dn(dn(dn(abspath(__file__)))), 'data') |
342f8434ed14
fixed creation of cache data dir, on first run
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
32 cache_dir = jn(cache_base, 'cache') |
342f8434ed14
fixed creation of cache data dir, on first run
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
33 |
342f8434ed14
fixed creation of cache data dir, on first run
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
34 if not os.path.isdir(cache_base): |
342f8434ed14
fixed creation of cache data dir, on first run
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
35 os.mkdir(cache_base) |
342f8434ed14
fixed creation of cache data dir, on first run
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
36 |
482
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
37 if not os.path.isdir(cache_dir): |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
38 os.mkdir(cache_dir) |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
39 # set start_time to current time |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
40 # to re-cache everything |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
41 # upon application startup |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
42 start_time = time.time() |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
43 # configure the "sqlalchemy" cache region. |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
44 cache_manager.regions['sql_cache_short'] = { |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
45 'type':'memory', |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
46 'data_dir':cache_dir, |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
47 'expire':10, |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
48 'start_time':start_time |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
49 } |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
50 cache_manager.regions['sql_cache_med'] = { |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
51 'type':'memory', |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
52 'data_dir':cache_dir, |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
53 'expire':360, |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
54 'start_time':start_time |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
55 } |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
56 cache_manager.regions['sql_cache_long'] = { |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
57 'type':'file', |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
58 'data_dir':cache_dir, |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
59 'expire':3600, |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
60 'start_time':start_time |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
61 } |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
62 #to use cache use this in query |
7afbc45aab28
added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents:
0
diff
changeset
|
63 #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |