3 _=\X2@sddlmZddlZddlZddlZddlZddlZddlZy ddlZ Wne k rdddl Z YnXy ddl Z Wne k rYnXej dZ GdddeZGdddeZGdd d eZGd d d eZGd d d eZGdddeZdS))print_functionNz tweepy.cachec@sDeZdZdZdddZddZddd Zd d Zd d ZddZ dS)CachezCache interface<cCs ||_dS)z[Initialize the cache timeout: number of seconds to keep a cached entry N)timeout)selfrrd/private/var/folders/47/550316q90kl84vt_jn5ctz8chwmm0c/T/pip-install-yoigbb1v/tweepy/tweepy/cache.py__init__szCache.__init__cCstdS)z\Add new record to cache key: entry key value: data of entry N)NotImplementedError)rkeyvaluerrrstore%sz Cache.storeNcCstdS)zGet cached entry if exists and not expired key: which entry to get timeout: override timeout with this value [optional] N)r )rr rrrrget,sz Cache.getcCstdS)z.Get count of entries currently stored in cacheN)r )rrrrcount3sz Cache.countcCstdS)z$Delete any expired entries in cache.N)r )rrrrcleanup7sz Cache.cleanupcCstdS)zDelete all cached entriesN)r )rrrrflush;sz Cache.flush)r)N) __name__ __module__ __qualname____doc__r r rrrrrrrrrs  rc@s\eZdZdZdddZddZddZd d Zd d ZdddZ ddZ ddZ ddZ d S) MemoryCachezIn-memory cachercCs tj||i|_tj|_dS)N)rr _entries threadingLocklock)rrrrrr Cs zMemoryCache.__init__cCs|j|jdS)N)entriesr)rr)rrrr __getstate__HszMemoryCache.__getstate__cCs"tj|_|d|_|d|_dS)Nrr)rrrrr)rstaterrr __setstate__Ls  zMemoryCache.__setstate__cCs|dkotj|d|kS)Nr)time)rentryrrrr _is_expiredRszMemoryCache._is_expiredcCs*|jjtj|f|j|<|jjdS)N)racquirerrrelease)rr r rrrr Us zMemoryCache.storeNc Cs^|jjzB|jj|}|s dS|dkr.|j}|j||rF|j|=dS|dS|jjXdS)N)rr"rrrr!r#)rr rr rrrrZs   zMemoryCache.getcCs t|jS)N)lenr)rrrrrsszMemoryCache.countc CsR|jjz6x0t|jjD]\}}|j||jr|j|=qWWd|jjXdS)N)rr"dictritemsr!rr#)rkvrrrrvs  zMemoryCache.cleanupcCs"|jj|jj|jjdS)N)rr"rclearr#)rrrrrs  zMemoryCache.flush)r)N) rrrrr rrr!r rrrrrrrrr@s   rc@seZdZdZiZd#ddZddZd$dd Zd d Zd%d d Z ddZ d&ddZ ddZ ddZ ddZd'ddZddZddZdd Zd!d"ZdS)( FileCachezFile-based cachercCstj||tjj|dkr&tj|||_|tjkrDtj||_ nt j |_ |j tj|<tj dkrv|j |_|j|_n6tj dkr|j|_|j|_ntjd|j|_|j|_dS)NFposixntz/FileCache locking not supported on this system!)rr ospathexistsmkdir cache_dirr+ cache_locksrrrname_lock_file_posix _lock_file_unlock_file_posix _unlock_file_lock_file_win32_unlock_file_win32logwarning_lock_file_dummy_unlock_file_dummy)rr2rrrrr s"          zFileCache.__init__cCs,tj}|j|jdtjj|j|jS)Nzutf-8) hashlibmd5updateencoder.r/joinr2 hexdigest)rr r@rrr _get_pathszFileCache._get_pathTcCsdS)Nr)rr/ exclusiverrrr=szFileCache._lock_file_dummycCsdS)Nr)rrrrrr>szFileCache._unlock_file_dummycCsb|d}|dkr*t|d}tj|tjnt|d}tj|tjtjj|dkr^|jdS|S)Nz.lockTwrF) openfcntllockfZLOCK_EXZLOCK_SHr.r/r0close)rr/rFZ lock_pathf_lockrrrr5s  zFileCache._lock_file_posixcCs |jdS)N)rL)rrrrrr7szFileCache._unlock_file_posixcCsdS)Nr)rr/rFrrrr9szFileCache._lock_file_win32cCsdS)Nr)rrrrrr:szFileCache._unlock_file_win32cCs,tj|tjj|dr(tj|ddS)Nz.lock)r.remover/r0)rr/rrr _delete_files zFileCache._delete_filec Csd|j|}|jjz>|j|}t|d}tjtj|f||j|j |Wd|jj XdS)Nwb) rErr"r6rIpickledumprrLr8r#)rr r r/rMdatafilerrrr s    zFileCache.storeNcCs|j|j||S)N)_getrE)rr rrrrrsz FileCache.getc Cstjj|dkrdS|jjzn|j|d}t|d}tj|\}}|j |dkrZ|j }|dkrt j ||krd}|j ||j ||S|jjXdS)NFrbr)r.r/r0rr"r6rIrQloadrLrrrOr8r#)rr/rrMrSZ created_timer rrrrTs"     zFileCache._getcCs2d}x(tj|jD]}|jdr"q|d7}qW|S)Nrz.lockr$)r.listdirr2endswith)rcr rrrrs   zFileCache.countcCs>x8tj|jD](}|jdrq|jtjj|j|dqWdS)Nz.lock)r.rWr2rXrTr/rC)rr rrrrs zFileCache.cleanupcCs<x6tj|jD]&}|jdrq|jtjj|j|qWdS)Nz.lock)r.rWr2rXrOr/rC)rr rrrrs zFileCache.flush)r)T)T)T)N)rrrrr3r rEr=r>r5r7r9r:rOr rrTrrrrrrrr+s"     r+c@sDeZdZdZdddZddZddd Zd d Zd d ZddZ dS) MemCacheCachezCache interfacercCs||_||_dS)zInitialize the cache client: The memcache client timeout: number of seconds to keep a cached entry N)clientr)rr[rrrrr szMemCacheCache.__init__cCs|jj|||jddS)z\Add new record to cache key: entry key value: data of entry )rN)r[setr)rr r rrrr szMemCacheCache.storeNcCs |jj|S)zGet cached entry if exists and not expired key: which entry to get timeout: override timeout with this value [optional]. DOES NOT WORK HERE )r[r)rr rrrrr!szMemCacheCache.getcCstdS)z8Get count of entries currently stored in cache. RETURN 0N)r )rrrrr)szMemCacheCache.countcCstdS)z*Delete any expired entries in cache. NO-OPN)r )rrrrr-szMemCacheCache.cleanupcCstdS)z Delete all cached entries. NO-OPN)r )rrrrr1szMemCacheCache.flush)r)N) rrrrr r rrrrrrrrrZs  rZc@sTeZdZdZdddZddZd d Zdd d ZddZddZ ddZ ddZ d S) RedisCachezCache running in a redis serverr tweepy:keystweepy:cCs"tj||||_||_||_dS)N)rr r[keys_containerpre_identifier)rr[rr`rarrrr 9s zRedisCache.__init__cCs|dkotj|d|kS)Nr)r)rr rrrrr!BszRedisCache._is_expiredcCsV|j|}|jj}|j|tjtj|f|j||j|j |j ||j dS)z-Store the key, value pair in our redis serverN) rar[pipeliner\rQdumpsrZexpirerZsaddr`execute)rr r piperrrr Fs   zRedisCache.storeNcCsX|j|}|jj|}|sdStj|}|dkr6|j}|j||rP|j|dS|dS)z4Given a key, returns an element from the redis tableNr$)rar[rrQloadsrr! delete_entry)rr rZunpickled_entryr rrrrVs     zRedisCache.getcCst|jj|jS)zNote: This is not very efficient, since it retreives all the keys from the redis server to know how many keys we have)r%r[smembersr`)rrrrrmszRedisCache.countcCs.|jj}|j|j||j||jdS)z%Delete an object from the redis tableN)r[rbZsremr`deleterd)rr rerrrrgss  zRedisCache.delete_entrycCsR|jj|j}x>|D]6}|jj|}|rtj|}|j||jr|j|qWdS)zCleanup all the expired keysN) r[rhr`rrQrfr!rrg)rkeysr r rrrrzs   zRedisCache.cleanupcCs*|jj|j}x|D]}|j|qWdS)z!Delete all entries from the cacheN)r[rhr`rg)rrjr rrrrs zRedisCache.flush)rr^r_)N) rrrrr r!r rrrgrrrrrrr]6s   r]c@sLeZdZdZdddZddZdd d Zd d Zd dZddZ ddZ dS) MongodbCachez*A simple pickle-based MongoDB cache sytem. tweepy_cachecCs0tj||||_|||_|jjd|ddS)z0Should receive a "database" cursor from pymongo.created)expireAfterSecondsN)rr rcol create_index)rdbrZ collectionrrrr s  zMongodbCache.__init__cCs<ddlm}tjj}|tj|}|jj|||ddS)Nr)Binary)rn_idr )Z bson.binaryrsdatetimeutcnowrQrcrpinsert)rr r rsnowZblobrrrr s  zMongodbCache.storeNcCs.|rt|jjd|i}|r*tj|dSdS)Nrtr )r rpZfind_onerQrf)rr robjrrrrs zMongodbCache.getcCs|jjijS)N)rpfindr)rrrrrszMongodbCache.countcCs|jjd|iS)Nrt)rprN)rr rrrrgszMongodbCache.delete_entrycCsdS)z.MongoDB will automatically clear expired keys.Nr)rrrrrszMongodbCache.cleanupcCs |jj|jjd|jddS)Nrn)ro)rpZdroprqr)rrrrrs zMongodbCache.flush)rlrm)N) rrrrr r rrrgrrrrrrrks  rk) __future__rrrur?rr.loggingcPicklerQ ImportErrorrJ getLoggerr;objectrrr+rZr]rkrrrrs,    $E 'U