Skip to content
This repository has been archived by the owner on Apr 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #14 from hadrien/sqlalchemy_fix
Browse files Browse the repository at this point in the history
Sqlalchemy fix
  • Loading branch information
hadrien committed Jun 5, 2014
2 parents 47379e6 + 340c297 commit 6f8d1f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
7 changes: 5 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Changelog
=========

Expand All @@ -10,12 +11,14 @@ Development
* Introspectables
* Content negociation: we should add content type in cache key.

* In ext.sqlalchemy: Increment new and deleted entities and table identities.

0.1.2
-----

* Breaking changes: ``cache_factory`` keyword arguments ``depends_on`` is a
list of callables which receiving request as only argument. Callables return
the dependency to be identified by cache manager.
list of callables which receiving request as positional argument. Callables
return the dependency to be identified by cache manager.
* Add arguments ``predicates`` to ``cache_factory`` which permits to add
predicates to cache key used for the view (useful for query strings).

Expand Down
13 changes: 9 additions & 4 deletions pyramid_caching/ext/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ def identify(entity):
return registry.queryAdapter(entity, IIdentityInspector)

def on_before_commit(session):
identities = []
identities = set()

for entity in session.new:
identities.add(entity.__tablename__)

for entity in session.dirty:
identities.append(entity.__tablename__)
identities.append(identify(entity))
identities.add(entity.__tablename__)
identities.add(identify(entity))

for entity in session.deleted:
identities.append(identify(entity))
identities.add(entity.__tablename__)
identities.add(identify(entity))

def after_commit(session):

for identity in identities:
try:
versioner.incr(identity)
Expand Down
21 changes: 18 additions & 3 deletions pyramid_caching/tests/unittests/test_sqlalchemy_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def setUp(self):
self.config = Configurator(settings={
'caching.enabled': True,
})

Session.add(User(name='hadrien', address='down the hill'))
Session.commit()

register_sqla_session_caching_hook(self.config, Session)
self.config.registry.registerAdapter(DummyIdentityInspector(),
required=[User],
Expand All @@ -57,7 +61,7 @@ def test_create_entity(self):
u = User(name='bob', address='123 street')
Session.add(u)
Session.commit()
self.assertEqual(self.key_versioner.incr_keys, [])
self.assertEqual(self.key_versioner.incr_keys, ['users'])

def test_modify_entity(self):
u = User(name='joe', address='123 street')
Expand All @@ -67,6 +71,13 @@ def test_modify_entity(self):
Session.commit()
self.assertEqual(self.key_versioner.incr_keys, ['users', 'users:joe'])

def test_delete_entity(self):
user = Session.query(User).filter_by(name='hadrien').first()
Session.delete(user)
Session.commit()
self.assertEqual(self.key_versioner.incr_keys,
['users', 'users:hadrien'])


class User(object):
__tablename__ = 'users'
Expand All @@ -84,7 +95,11 @@ def __call__(self, entity):

class DummyKeyVersioner:
def __init__(self):
self.incr_keys = []
self._incr_keys = set()

def incr(self, key):
self.incr_keys.append(key)
self._incr_keys.add(key)

@property
def incr_keys(self):
return list(self._incr_keys)

0 comments on commit 6f8d1f4

Please sign in to comment.