mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-02-10 06:35:03 -05:00
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:
37
r2/r2/models/last_modified.py
Normal file
37
r2/r2/models/last_modified.py
Normal 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)
|
||||
Reference in New Issue
Block a user