Permalink
Browse files

Updated sqla to create the table lazily.

  • Loading branch information...
1 parent 63dfdef commit d620c2f3928b510d1b7534146294b1d5b25dc365 @mmerickel mmerickel committed Mar 13, 2012
Showing with 11 additions and 5 deletions.
  1. +11 −5 anykeystore/backends/sqla.py
@@ -48,13 +48,19 @@ def __init__(self, url, **kw):
)
kw['url'] = url
self.engine = engine_from_config(kw, prefix='')
- self._create_table() # TODO: ideally delay this until first access
def _create_table(self):
self.table.create(checkfirst=True, bind=self.engine)
+ self._created = True
+
+ _created = False
+ def _get_conn(self):
+ if not self._created:
+ self._create_table()
+ return self.engine.connect()
def retrieve(self, key):
- c = self.engine.connect()
+ c = self._get_conn()
try:
data = c.execute(select(
[self.table.c.value, self.table.c.expires],
@@ -71,22 +77,22 @@ def store(self, key, value, expires=None):
expiration = None
if expires is not None:
expiration = datetime.utcnow() + coerce_timedelta(expires)
- c = self.engine.connect()
+ c = self._get_conn()
try:
c.execute(
self.table.insert(), key=key, value=value, expires=expiration)
finally:
c.close()
def delete(self, key):
- c = self.engine.connect()
+ c = self._get_conn()
try:
c.execute(delete(self.table, self.table.c.key == key))
finally:
c.close()
def purge_expired(self):
- c = self.engine.connect()
+ c = self._get_conn()
try:
c.execute(
delete(self.table, self.table.c.expires < datetime.utcnow()))

0 comments on commit d620c2f

Please sign in to comment.