1
我有很多類都做同樣的事情:他們在構建過程中收到一個標識符(DB中的PK),然後從DB加載。 我想緩存這些類的實例,以最大限度地減少對數據庫的調用。當緩存達到臨界尺寸時,應放棄最近訪問過的緩存對象。緩存和內存使用
緩存實際上似乎工作正常,但不知何故我無法確定緩存的內存使用情況(在#Next line doesn't do what I expected
之後的行中)。
我迄今爲止代碼:
#! /usr/bin/python3.2
from datetime import datetime
import random
import sys
class Cache:
instance = None
def __new__ (cls):
if not cls.instance:
cls.instance = super().__new__ (cls)
cls.instance.classes = {}
return cls.instance
def getObject (self, cls, ident):
if cls not in self.classes: return None
cls = self.classes [cls]
if ident not in cls: return None
return cls [ident]
def cache (self, object):
#Next line doesn't do what I expected
print (sys.getsizeof (self.classes))
if object.__class__ not in self.classes:
self.classes [object.__class__] = {}
cls = self.classes [object.__class__]
cls [object.ident] = (object, datetime.now())
class Cached:
def __init__ (self, cache):
self.cache = cache
def __call__ (self, cls):
cls.cache = self.cache
oNew = cls.__new__
def new (cls, ident):
cached = cls.cache().getObject (cls, ident)
if not cached: return oNew (cls, ident)
cls.cache().cache (cached [0])
return cached [0]
cls.__new__ = new
def init (self, ident):
if hasattr (self, 'ident'): return
self.ident = ident
self.load()
cls.__init__ = init
oLoad = cls.load
def load (self):
oLoad (self)
self.cache().cache (self)
cls.load = load
return cls
@Cached (Cache)
class Person:
def load (self):
print ('Expensive call to DB')
print ('Loading Person {}'.format (self.ident))
#Just simulating
self.name = random.choice (['Alice', 'Bob', 'Mallroy'])
@Cached (Cache)
class Animal:
def load (self):
print ('Expensive call to DB')
print ('Loading Animal {}'.format (self.ident))
#Just simulating
self.species = random.choice (['Dog', 'Cat', 'Iguana'])
sys.getsizeof
回報搞笑值。
如何確定所有緩存對象的實際內存使用情況?