/usr/share/pyshared/freevo/util/objectcache.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# objectcache.py - A cache for objects identified by a string
# -----------------------------------------------------------------------
# $Id: objectcache.py 11752 2010-11-28 08:57:02Z adam $
#
# Notes:
# Todo:
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------
from threading import RLock
import config
class ObjectCache:
'''Provides a cache for objects indexed by a string. It should
be slow for a large number of objects, since searching takes
O(n) time. The cachesize given in the constructor is the
maximum number of objects in the cache. Objects that have not
been accessed for the longest time get deleted first.'''
def __init__(self, cachesize=30, desc='noname'):
self.cache = {}
self.lru = [] # Least recently used (lru) list, index 0 is lru
self.cachesize = cachesize
self.desc = desc
self.lock = RLock()
def keys(self):
self.lock.acquire()
keys = self.cache.keys()
self.lock.release()
return keys
def __contains__(self, key):
self.lock.acquire()
result = key in self.cache
self.lock.release()
return result
def __getitem__(self, key):
self.lock.acquire()
if isinstance(key, str):
key = unicode(key, config.LOCALE)
try:
del self.lru[self.lru.index(key)]
self.lru.append(key)
result = self.cache[key]
except:
result = None
self.lock.release()
return result
def __setitem__(self, key, object):
self.lock.acquire()
if isinstance(key, str):
key = unicode(key, config.LOCALE)
try:
# remove old one if key is already in cache
del self.lru[self.lru.index(key)]
except:
pass
# Do we need to delete the oldest item?
if len(self.cache) > self.cachesize:
# Yes
lru_key = self.lru[0]
del self.cache[lru_key]
del self.lru[0]
self.cache[key] = object
self.lru.append(key)
self.lock.release()
def __delitem__(self, key):
if isinstance(key, str):
key = unicode(key, config.LOCALE)
if not key in self.cache:
return
self.lock.acquire()
del self.cache[key]
del self.lru[self.lru.index(key)]
self.lock.release()
#
# Simple test...
#
if __name__ == '__main__':
cache = ObjectCache(2)
cache['1'] = 'one'
cache['2'] = 'zwei'
cache['3'] = 'drei'
cache['3'] = 'three'
# delete something
del cache['2']
# delete an item that is not in the list
del cache['2']
cache['2'] = 'two'
# One should have been overwritten
# '2' should be 'two
# '3' should be 'three'
if cache['1'] != None:
print "Test failed."
print "two - three == %s - %s" % (cache['2'], cache['3'])
|