Add model for new Last Modified system.

This will replace the current stuff in thing_utils.py. The idea is that
by storing the various last modified timestamps for a thing in a single
row together, we can save Cassandra round trips and seeks per request.
This commit is contained in:
Neil Williams
2012-05-24 19:46:35 -07:00
parent ba54ed713a
commit 7f3f84494e

View File

@@ -0,0 +1,37 @@
import datetime
from pylons import g
from pycassa.system_manager import ASCII_TYPE, DATE_TYPE
from r2.lib.db import tdb_cassandra
from r2.lib.utils import tup
class LastModified(tdb_cassandra.View):
_use_db = True
_value_type = "date"
_connection_pool = "main"
_read_consistency_level = tdb_cassandra.CL.ONE
_extra_schema_creation_args = dict(key_validation_class=ASCII_TYPE,
default_validation_class=DATE_TYPE)
@classmethod
def touch(cls, fullname, names):
names = tup(names)
now = datetime.datetime.now(g.tz)
values = dict.fromkeys(names, now)
cls._set_values(fullname, values)
return now
@classmethod
def get(cls, fullname, name, touch_if_not_set=False):
try:
obj = cls._byID(fullname)
except tdb_cassandra.NotFound:
if touch_if_not_set:
time = cls.touch(fullname, name)
return time
else:
return None
return getattr(obj, name, None)