diff options
author | Rob Austein <sra@hactrn.net> | 2012-08-29 20:55:58 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2012-08-29 20:55:58 +0000 |
commit | e252a23ca2502fa83d234bb4491efae02aeec30d (patch) | |
tree | 62a8bf04621d2e2d5dfbfb9aedbec79d8ca4b30f /rpkid/rpki/sql.py | |
parent | 3e72d90ded7b35d3b292acfd1989c0ebe45174d6 (diff) |
Add a lot of __repr__() methods in an attempt to make the logs more
useful. Add rpki.sql.cache_reference decorator, to give the garbage
collector some guidance about linkages between active objects now that
the SQL cache uses weak references. Other minor cleanup.
svn path=/branches/tk274/; revision=4676
Diffstat (limited to 'rpkid/rpki/sql.py')
-rw-r--r-- | rpkid/rpki/sql.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/rpkid/rpki/sql.py b/rpkid/rpki/sql.py index 553bea83..1bb607cc 100644 --- a/rpkid/rpki/sql.py +++ b/rpkid/rpki/sql.py @@ -374,3 +374,32 @@ class sql_persistent(object): """ pass + +def cache_reference(func): + """ + Decorator for use with property methods which just do an SQL lookup based on an ID. + Check for an existing reference to the object, just return that if we find it, + otherwise perform the SQL lookup. + + Not 100% certain this is a good idea, but I //think// it should work well with the + current weak reference SQL cache, so long as we create no circular references. + So don't do that. + """ + + attr_name = "_" + func.__name__ + + def wrapped(self): + try: + value = getattr(self, attr_name) + assert value is not None + except AttributeError: + value = func(self) + if value is not None: + setattr(self, attr_name, value) + return value + + wrapped.__name__ = func.__name__ + wrapped.__doc__ = func.__doc__ + wrapped.__dict__.update(func.__dict__) + + return wrapped |