class MemoryCache: def __init__(self, persistantDB, cacheDB): self.persistantDB = persistantDB self.cacheDB = cacheDB
def get(self, modeltype, *args, **kwargs): try: with Using(self.cacheDB, [modeltype]): return modeltype.get(*args, **kwargs) except DoesNotExist as e: r = None with Using(self.persistantDB, [modeltype]): r = modeltype.get(*args, **kwargs) self.cache(r) return r
def cache(self, model): with Using(self.cacheDB, [type(model)]): return model.save()
def persist(self, model): with Using(self.persistantDB, [type(model)]): model.save()
def uncache(self, model): self.persist(model) with Using(self.cacheDB, [type(model)]): return model.delete_instance()
db = SqliteDatabase("on-disk.sqlite")db.connect()database_proxy.initialize(db)db.create_tables([Account, Character, Equipment, Friend, Instance, Inventory, Mail, Mission, NPC, Object, Session, Topic, World], True)
cacheDB = SqliteDatabase(':memory:')cacheDB.connect()with Using(cacheDB, [Account, Character, Equipment, Friend, Instance, Inventory, Mail, Mission, NPC, Object, Session, Topic, World]): cacheDB.create_tables([Account, Character, Equipment, Friend, Instance, Inventory, Mail, Mission, NPC, Object, Session, Topic, World])
memoryCache = MemoryCache(db, cacheDB)
#Test the cachea = Session()a.sessionKey = "test"memoryCache.cache(a) #<-- This line errors with: "no such table: session"--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yes, in memory databases only work for the lifetime of the connection. So you need to maintain a single, active connection. You can share memory databases between conns, but youll need to read the sqlite documentation.
cacheDB = SqliteDatabase(':memory:')
cacheDB = SqliteDatabase('file:cachedb?mode=memory&cache=shared', uri=True)
from peewee import SqliteDatabase, Model, IntegerField
class InstanceMeta:
def __init__(self, class_meta, database):
self.class_meta = class_meta
self.database = database
def __getattr__(self, name):
if name == 'database':
return self.database
else:
return getattr(self.class_meta, name)
class InstanceModel(Model):
""" Potentially unique DB per model instance. """
# Store created dbs so can return same database objects.
db_cache = {}
# Database paths that should not be cached.
# These will always return a new database object
no_db_cache = [None, ':memory:']
def __init__(self, db_path=None, *args, **kwargs):
database = self.create_database(db_path)
self._meta = InstanceMeta(class_meta=self._meta,
database=database)
super().__init__(*args, **kwargs)
@staticmethod
def create_database(db_path=None):
factory = SqliteDatabase
if not db_path:
db_path = ':memory:'
# Some paths should never be cached.
if db_path in InstanceModel.no_db_cache:
return factory(db_path)
# Otherwise, there's always a cache.
cache = InstanceModel.db_cache
if db_path not in cache:
cache[db_path] = factory(db_path)
return cache[db_path]
# Example model
class MyModel(InstanceModel):
x = IntegerField()
y = IntegerField()
# Illustrative test cases
class TestMultipleModelInstances:
""" Test various configurations of multiple instances of same model. """
def test_multiple_dbs(self):
""" Providing multiple db paths should result in each model pointing to a different database. """
first = MyModel('/tmp/first.db')
second = MyModel('/tmp/second.db')
assert first._meta.database is not second._meta.database
assert first._meta.database.database == '/tmp/first.db'
assert second._meta.database.database == '/tmp/second.db'
def test_same_db(self):
""" Providing the same db string multiple times should result in the same database object. """
first = MyModel('/tmp/first.db')
second = MyModel('/tmp/first.db')
assert first._meta.database is second._meta.database
def test_multiple_in_memory(self):
""" In memory databases should always be unique. """
first = MyModel()
second = MyModel()
assert first._meta.database is not second._meta.database
In memory databases only exist for a single connection. My guess is you're attempting to reconnect?