m &Ec@szdZdklZlZdklZdklZdkl Z dk Z dk l Z dk l Z dklZd klZyd klZlZWnd klZlZnXgZd efd YZd fdYZdfdYZdefdYZhZeiddZdeefdYZ e e dfdYZ!e de"Z#ddZ%dS(sI RAM cache manager -- Caches the results of method calls in RAM. $Id$ (sCaches CacheManager(s SimpleItem(s allocate_lock(sescapeN(sInitializeClass(sDTMLFile(sClassSecurityInfo(sview_management_screens(sPicklersHIGHEST_PROTOCOLtCacheExceptioncBstZdZRS(s$ A cache-related exception. (t__name__t __module__t__doc__(((tM/data/zmath/zope/lib/python/Products/StandardCacheManagers/RAMCacheManager.pyR%s t CacheEntrycBstZdZdZRS(s$ Represents a cached value. cCsyEt}t|t}|i||i||i|_ Wnt dnXt i |_ ||_||_d|_dS(Ns)The data for the cache is not pickleable.i(t _ByteCountertsizertPicklertHIGHEST_PROTOCOLtpicklertdumptindextdatatgetCounttselftsizeRttimetcreatedt view_namet access_count(RR R RRR ((Rt__init__0s     (RRRR(((RR+s tObjectCacheEntriescBsGtZdZdZdZdZdZdZdZdZ RS(s3 Represents the cache for one Zope object. icCs||_d|_h|_dS(Ni(tpathRt physical_pathtlastmodtentries(RR((RRQs  c Csg}xV|D]N}|djo d}n|i|d}|it|t|fq W|oPg}x9|i D]+\}}|it|t|fqyW|i nf}t|t|t|fS(s Returns the index to be used when looking for or inserting a cache entry. view_name is a string. local_keys is a mapping or None. tN(t req_indext req_namestkeytreqtNonetvaltgettappendtstrt local_keyst local_indextitemstsortRttuple( RRRRR%RR!R&R((RtaggregateIndexVs   # #cCs=|i|joh|_||_tSn|ii|tS(N(RRRt_markerR"R (RRR ((RtgetEntryns   cCs&||_t||||i|R3R=R((RtgetObjectCacheEntriess   cCs7d}x*|iiD]}|t|i}qW|S(s9 Returns the count of all cache entries. iN(tcountRR3tvaluesR=tlenR(RR@R=((RtcountAllEntriess cCsah}xT|iiD]C}x:|iiD])}|i}|i|dd|| number of entries accessed (n) times iiN( tcountersRR3RAR=RtentryRR"(RRER=RRD((Rt countAccessess "cCsAx:|iiD])}x |iiD]}d|_q&WqWdS(s; Clears access_count for each cache entry. iN(RR3RAR=RRER(RRER=((RtclearAccessCounterss cCs|iizx|iiD]n\}}x;|iiD]*\}}|i |jo|i|=q<q<Wt |idjo|i|=q q WWd|ii XdS(sF Deletes entries that haven't been accessed recently. iN(RR5tacquireR3R'tpR=RtagindexRERtthreshold_access_countRBtrelease(RRKR=RJRIRE((RtdeleteEntriesAtOrBelowThresholds   cCs|idjo|iizti|i}x|iiD]n\}}x;|i iD]*\}}|i |jo|i |=q_q_Wt|i djo|i|=qCqCWWd|iiXndS(s4 Deletes entries that have expired. iiN(Rtmax_ageR5RHRt min_createdR3R'RIR=RRJRERRBRL(RROR=RJRIRE((RtdeleteStaleEntriess   cCs|i|i}||ijo|i}|i}t |djoa|i d}x3|D]+\}}||}||ijoPqhqhW|i ||iqndS(s( Removes cache entries. iN(RRPRCt new_countt thresholdRFRDR't prioritiesRBR(RteffectRMRG(RRRTRSRQRD((Rtcleanups          c Csg}x|iiD]}d}d}g}x_|iiD]N}||i}||i }|i pd}||jo|i |q>q>W|ihddi|i<d|i<d|i<d|<d|<d |<d t|i<}|i |qW|S( s7 Reports on the contents of the cache. is Rt/R0R1RtcountertviewsRN(trvalRR3RAR=RtacRXRRERRtviewR#R(tjoinRR0R1RBtinfo( RR]RZRXRYR=RER[R((RtgetCacheReports$    `cCs|i}|iizRxK|iiD]:\}}|i }|t | |jo|i|=q,q,WWd|ii XdS(sA Invalidates the cache entries that apply to ob. N(R;R<RRR5RHR3R'RIR=RtppRBRL(RR;R_R=RIR((RtZCache_invalidate s    Rc Cs|i|}|djo|Sn|i|} |i ||i |i |}|i| |}|tjo|Sn|idjoT|iti|ijo7|iiz|i|Wd|iiX|Sn|id|_|id|_|iS(s8 Gets a cache entry or returns default. iNi(RR?R;R=R tdefaulttZCacheable_getModTimet mtime_funcRR*RtREQUESTt request_varstkeywordsR R,RER+RNRRR5RHR/RLR0RR ( RR;RRfRcRaR R=RER((Rt ZCache_gets(  - c Csti} |i| jo|i| |i|_n|i|}|i i z]|i |dd}|i||i|i|}|i|||||id|_Wd|i iXdS(s% Sets a cache entry. R>iN(RtnowRR6RUtcleanup_intervalR;RbRcRR5RHR?R=R*RRdReRfR R-R R1RL( RR;R RRfRcR RR=Rh((Rt ZCache_set6s   (RRRNRR:R?RCRFRGRMRPRUR^R`R RgRj(((RR2s         t.itRAMCacheManagercBs}tZdZeZeiddhdd<dd<dedfsN( RRRRRtcR^RYR(treverse(RRRRYR((RR^s  t sort_linkcCs}|i\}}|id|}d}||jo | }n|d|odpd}dt|dt|fS( s5 Utility for generating a sort link. s/manage_stats?sort_by=is&sort_reverse=t1t0s%siN( RRRRt absolute_urlRwturltnewsrtescapetname(RRRwRRRR((RRs  tmanage_invalidatecCsuxG|D]?}y|i|}Wnttfj onX|iqW|dj od}|i d|SndS(s# ZMI helper to invalidate an entry sCache entries invalidatedRN( tpathsRRtunrestrictedTraverseR;tAttributeErrorR.tZCacheable_invalidateRdR tmsgRt(RRRdR;RR((RRs (Rm(RRRtClassSecurityInfotsecuritytsetPermissionDefaultt PRODUCT_DIRt CacheManagertmanage_optionst SimpleItemt meta_typeRR|tZCacheManager_getCache__roles__RtdeclareProtectedtview_management_screensRtDTMLFiletglobalsRqR RRtRR^RR(((RRlNs0  \      RcBs&tZdZdZdZdZRS(s>auxiliary file like class which just counts the bytes written.icCs|it|7_dS(N(Rt_countRBtbytes(RR((RtwritescCs|iS(N(RR(R((RRs(RRRRRR(((RRs  s dtml/addRCMcCs;|i|t||dj o|i||SndS(s'Adds a RAM cache manager to the folder.N(Rt _setObjectRwRlRdR Rq(RRwRd((Rtmanage_addRAMCacheManagers (&Rt OFS.CachetCacheRtOFS.SimpleItemRtthreadR4tcgiRRtGlobalstInitializeClassRt AccessControlRtAccessControl.PermissionsRtcPickleRR tpickleR+t ExceptionRRRR2R~RtsplitRRlRRtmanage_addRAMCacheManagerFormR R(R RRR+RRRRRRRRRRRRRR2R4RRRlR~((Rt?s2        6