annotate pylons_app/model/meta.py @ 482:7afbc45aab28 celery

added caching queries to hg-app
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 17 Sep 2010 21:35:46 +0200
parents 564e40829f80
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 """SQLAlchemy Metadata and Session object"""
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2 from sqlalchemy.ext.declarative import declarative_base
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
3 from sqlalchemy.orm import scoped_session, sessionmaker
482
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
4 from pylons_app.model import caching_query
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
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
12
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
13 __all__ = ['Base', 'Session']
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14 #
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
15 # SQLAlchemy session manager. Updated by model.init_model()
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
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
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
22
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
23 # The declarative Base
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
24 Base = declarative_base()
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
25 #For another db...
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
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 #===============================================================================
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
31 cache_dir = jn(dn(dn(dn(abspath(__file__)))), 'data', 'cache')
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
32 if not os.path.isdir(cache_dir):
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
33 os.mkdir(cache_dir)
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
34 # set start_time to current time
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
35 # to re-cache everything
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
36 # upon application startup
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
37 start_time = time.time()
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
38 # configure the "sqlalchemy" cache region.
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
39 cache_manager.regions['sql_cache_short'] = {
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
40 'type':'memory',
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
41 'data_dir':cache_dir,
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
42 'expire':10,
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
43 'start_time':start_time
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
44 }
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
45 cache_manager.regions['sql_cache_med'] = {
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
46 'type':'memory',
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
47 'data_dir':cache_dir,
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
48 'expire':360,
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
49 'start_time':start_time
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
50 }
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
51 cache_manager.regions['sql_cache_long'] = {
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
52 'type':'file',
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
53 'data_dir':cache_dir,
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
54 'expire':3600,
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
55 'start_time':start_time
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
56 }
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
57 #to use cache use this in query
7afbc45aab28 added caching queries to hg-app
Marcin Kuzminski <marcin@python-works.com>
parents: 0
diff changeset
58 #.options(FromCache("sqlalchemy_cache_type", "cachekey"))