home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import datetime
- import threading
- import time
- import cherrypy
- from cherrypy.lib import cptools, http
-
- class MemoryCache:
- maxobjects = 1000
- maxobj_size = 100000
- maxsize = 10000000
- delay = 600
-
- def __init__(self):
- self.clear()
- t = threading.Thread(target = self.expire_cache, name = 'expire_cache')
- self.expiration_thread = t
- if hasattr(threading.Thread, 'daemon'):
- t.daemon = True
- else:
- t.setDaemon(True)
- t.start()
-
-
- def clear(self):
- self.cache = { }
- self.expirations = { }
- self.tot_puts = 0
- self.tot_gets = 0
- self.tot_hist = 0
- self.tot_expires = 0
- self.tot_non_modified = 0
- self.cursize = 0
-
-
- def key(self):
- return cherrypy.url(qs = cherrypy.request.query_string)
-
-
- def expire_cache(self):
- while time:
- now = time.time()
- for expiration_time, objects in self.expirations.items():
- if expiration_time <= now:
- for obj_size, obj_key in objects:
-
- try:
- del self.cache[obj_key]
- self.tot_expires += 1
- self.cursize -= obj_size
- continue
- except KeyError:
- continue
-
-
-
- del self.expirations[expiration_time]
- continue
- None<EXCEPTION MATCH>KeyError
-
- time.sleep(0.1)
-
-
- def get(self):
- self.tot_gets += 1
- cache_item = self.cache.get(self.key(), None)
- if cache_item:
- self.tot_hist += 1
- return cache_item
- return None
-
-
- def put(self, obj):
- if len(self.cache) < self.maxobjects:
- obj_size = len(obj[2])
- total_size = self.cursize + obj_size
- if obj_size < self.maxobj_size and total_size < self.maxsize:
- expiration_time = cherrypy.response.time + self.delay
- obj_key = self.key()
- bucket = self.expirations.setdefault(expiration_time, [])
- bucket.append((obj_size, obj_key))
- self.cache[obj_key] = obj
- self.tot_puts += 1
- self.cursize = total_size
-
-
-
-
- def delete(self):
- self.cache.pop(self.key(), None)
-
-
-
- def get(invalid_methods = ('POST', 'PUT', 'DELETE'), **kwargs):
- request = cherrypy.request
- if request.method in invalid_methods:
- cherrypy._cache.delete()
- request.cached = False
- request.cacheable = False
- return False
- cache_data = cherrypy._cache.get()
- request.cacheable = not c
- if c:
- response = cherrypy.response
- (s, h, b, create_time, original_req_headers) = cache_data
- for header_element in h.elements('Vary'):
- key = header_element.value
- if original_req_headers[key] != request.headers.get(key, 'missing'):
- request.cached = False
- request.cacheable = True
- return False
-
- for k in h:
- dict.__setitem__(rh, k, dict.__getitem__(h, k))
-
- response.headers['Age'] = str(int(response.time - create_time))
-
- try:
- cptools.validate_since()
- except cherrypy.HTTPRedirect:
- original_req_headers[key] != request.headers.get(key, 'missing')
- x = original_req_headers[key] != request.headers.get(key, 'missing')
- request.cached = c = bool(cache_data)
- raise
- except:
- None if x.status == 304 else request.method in invalid_methods
-
- response.status = s
- response.body = b
-
- return c
-
-
- def tee_output():
-
- def tee(body):
- output = []
- for chunk in body:
- output.append(chunk)
- yield chunk
-
-
- response = cherrypy.response
- response.body = tee(response.body)
-
-
- def expires(secs = 0, force = False):
- response = cherrypy.response
- headers = response.headers
- cacheable = False
- if not force:
- for indicator in ('Etag', 'Last-Modified', 'Age', 'Expires'):
- if indicator in headers:
- cacheable = True
- break
- continue
-
-
- if not cacheable:
- if isinstance(secs, datetime.timedelta):
- secs = 86400 * secs.days + secs.seconds
-
- if secs == 0:
- if force or 'Pragma' not in headers:
- headers['Pragma'] = 'no-cache'
-
- if cherrypy.request.protocol >= (1, 1):
- if force or 'Cache-Control' not in headers:
- headers['Cache-Control'] = 'no-cache, must-revalidate'
-
-
- expiry = http.HTTPDate(1.16994e+09)
- else:
- expiry = http.HTTPDate(response.time + secs)
- if force or 'Expires' not in headers:
- headers['Expires'] = expiry
-
-
-
-