evennia/src/cache/models.py
Griatch 1ea7e69821 Implemented persistent cache, events and gametime counter.
OBS - there is a new data table (for the persistent cache) so you need to sync or restart with your database.

* Persistent cache (pcache)- this works the same as the volatile cache, except it is regularly saved to disk and recovered upon restart. How often the pcache is backed up is set in preferences. This was heck of a tricky thing to get right due to the intricacies of pickle; for example it turns out there is a bug in cPickle, so only normal pickle works to store the cache objects.
* Persistent events - this makes use of the pcache to re-load the scheduled events every reload. Only events with the property "persistent" will be saved this way (if not set, events will get lost upon reboot, just like now). All the main system events have been implemented as persistent events, including a new event to regularly save the pcache to disk.
* In order to track persistent event timers across reboots, there is also a global "game time" defined now. This is saved in cache and counts seconds only when the server is running. Event timers are adjusted with an offset when restarting (otherwise they will be confused by the real time jumping forward after a downtime). There are also a small set of helpful routines in src/gametime.py to help convert from real time to game time (for easy creation of new events).
* Various info commands have been updated to incoorporate the time stamp and the cache sync information.
* There are a few test commands commented out in commands/general.py that I used for testing; I left them in if you want to test things quickly. It works here, but as always more people testing is needed.
/Griatch
2009-11-22 21:18:55 +00:00

56 lines
1.6 KiB
Python

"""
This implements a database storage cache for storing global
cache data persistently.
It is intended to be used with an event timer for updating
semi-regularly (otherwise, object attributes are better to use
if full persistency is needed).
"""
from django.db import models
from django.conf import settings
from src.cache.managers.cache import CacheManager
# 091120 - there is a bug in cPickle for importing the
# custom cache objects; only normal pickle works. /Griatch
import pickle
#try:
# import cPickle as pickle
#except ImportError:
# import pickle
class PersistentCache(models.Model):
"""
Implements a simple pickled database object, without
using the in-game object attribute model.
"""
cache_name = models.CharField(max_length=255)
cache_data = models.TextField(blank=True)
objects = CacheManager()
class Meta:
permissions = settings.PERM_CACHE
def load_cache(self):
"""
Recovers cache from database storage.
"""
cache_data = str(self.cache_data)
#print "loading cache: %s" % cache_data
if cache_data:
cache_data = pickle.loads(cache_data)
cache_data.pickle_no()
return cache_data
else:
return None
def save_cache(self, cache_obj):
"""
Stores a cache as a pickle.
"""
#print "saving ... '%s': %s" % (cache_obj,cache_obj.show())
cache_obj.pickle_yes()
self.cache_data = pickle.dumps(cache_obj)
cache_obj.pickle_no()
self.save()