<s> "<STR_LIT>" <EOL> import os <EOL> import time <EOL> import shutil <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> from django . core . cache . backends . base import BaseCache <EOL> from django . utils . hashcompat import md5_constructor <EOL> class CacheClass ( BaseCache ) : <EOL> def __init__ ( self , dir , params ) : <EOL> BaseCache . __init__ ( self , params ) <EOL> max_entries = params . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> try : <EOL> self . _max_entries = int ( max_entries ) <EOL> except ( ValueError , TypeError ) : <EOL> self . _max_entries = <NUM_LIT> <EOL> cull_frequency = params . get ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> try : <EOL> self . _cull_frequency = int ( cull_frequency ) <EOL> except ( ValueError , TypeError ) : <EOL> self . _cull_frequency = <NUM_LIT:3> <EOL> self . _dir = dir <EOL> if not os . path . exists ( self . _dir ) : <EOL> self . _createdir ( ) <EOL> def add ( self , key , value , timeout = None ) : <EOL> self . validate_key ( key ) <EOL> if self . has_key ( key ) : <EOL> return False <EOL> self . set ( key , value , timeout ) <EOL> return True <EOL> def get ( self , key , default = None ) : <EOL> self . validate_key ( key ) <EOL> fname = self . _key_to_file ( key ) <EOL> try : <EOL> f = open ( fname , '<STR_LIT:rb>' ) <EOL> try : <EOL> exp = pickle . load ( f ) <EOL> now = time . time ( ) <EOL> if exp < now : <EOL> self . _delete ( fname ) <EOL> else : <EOL> return pickle . load ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> except ( IOError , OSError , EOFError , pickle . PickleError ) : <EOL> pass <EOL> return default <EOL> def set ( self , key , value , timeout = None ) : <EOL> self . validate_key ( key ) <EOL> fname = self . _key_to_file ( key ) <EOL> dirname = os . path . dirname ( fname ) <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> self . _cull ( ) <EOL> try : <EOL> if not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> f = open ( fname , '<STR_LIT:wb>' ) <EOL> try : <EOL> now = time . time ( ) <EOL> pickle . dump ( now + timeout , f , pickle . HIGHEST_PROTOCOL ) <EOL> pickle . dump ( value , f , pickle . HIGHEST_PROTOCOL ) <EOL> finally : <EOL> f . close ( ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def delete ( self , key ) : <EOL> self . validate_key ( key ) <EOL> try : <EOL> self . _delete ( self . _key_to_file ( key ) ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def _delete ( self , fname ) : <EOL> os . remove ( fname ) <EOL> try : <EOL> dirname = os . path . dirname ( fname ) <EOL> os . rmdir ( dirname ) <EOL> os . rmdir ( os . path . dirname ( dirname ) ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def has_key ( self , key ) : <EOL> self . validate_key ( key ) <EOL> fname = self . _key_to_file ( key ) <EOL> try : <EOL> f = open ( fname , '<STR_LIT:rb>' ) <EOL> try : <EOL> exp = pickle . load ( f ) <EOL> now = time . time ( ) <EOL> if exp < now : <EOL> self . _delete ( fname ) <EOL> return False <EOL> else : <EOL> return True <EOL> finally : <EOL> f . close ( ) <EOL> except ( IOError , OSError , EOFError , pickle . PickleError ) : <EOL> return False <EOL> def _cull ( self ) : <EOL> if int ( self . _num_entries ) < self . _max_entries : <EOL> return <EOL> try : <EOL> filelist = sorted ( os . listdir ( self . _dir ) ) <EOL> except ( IOError , OSError ) : <EOL> return <EOL> if self . _cull_frequency == <NUM_LIT:0> : <EOL> doomed = filelist <EOL> else : <EOL> doomed = [ os . path . join ( self . _dir , k ) for ( i , k ) in enumerate ( filelist ) if i % self . _cull_frequency == <NUM_LIT:0> ] <EOL> for topdir in doomed : <EOL> try : <EOL> for root , _ , files in os . walk ( topdir ) : <EOL> for f in files : <EOL> self . _delete ( os . path . join ( root , f ) ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def _createdir ( self ) : <EOL> try : <EOL> os . makedirs ( self . _dir ) <EOL> except OSError : <EOL> raise EnvironmentError ( "<STR_LIT>" % self . _dir ) <EOL> def _key_to_file ( self , key ) : <EOL> """<STR_LIT>""" <EOL> path = md5_constructor ( key . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> path = os . path . join ( path [ : <NUM_LIT:2> ] , path [ <NUM_LIT:2> : <NUM_LIT:4> ] , path [ <NUM_LIT:4> : ] ) <EOL> return os . path . join ( self . _dir , path ) <EOL> def _get_num_entries ( self ) : <EOL> count = <NUM_LIT:0> <EOL> for _ , _ , files in os . walk ( self . _dir ) : <EOL> count += len ( files ) <EOL> return count <EOL> _num_entries = property ( _get_num_entries ) <EOL> def clear ( self ) : <EOL> try : <EOL> shutil . rmtree ( self . _dir ) <EOL> except ( IOError , OSError ) : <EOL> pass </s>
