Ñò ha-Kc @s&dZddkZddkZddkZyddkZWnej oddkZnXdZdZdZ dZ de fd„ƒYZ d fd „ƒYZ d fd „ƒYZd „Zedjo\dGHeƒdGHdgZe eddƒZd„Zd„Zdfd„ƒYZeddƒeddƒededd>ƒƒo'dGHeidƒo dGHnd GHnd!GHeiddgƒGHd"GHeeid#ƒƒGHeƒZed$eƒd%GHeiddƒZed&jo dGHnd GHd'GHeiddƒZedjo dGHnd GHndS((sÖ client module for memcached (memory cache daemon) Overview ======== See U{the MemCached homepage} for more about memcached. Usage summary ============= This should give you a feel for how this module operates:: import memcache mc = memcache.Client(['127.0.0.1:11211'], debug=0) mc.set("some_key", "Some value") value = mc.get("some_key") mc.set("another_key", 3) mc.delete("another_key") mc.set("key", "1") # note that the key used for incr/decr must be a string. mc.incr("key") mc.decr("key") The standard way to use memcache with a database is like this:: key = derive_key(obj) obj = mc.get(key) if not obj: obj = backend_api.get(...) mc.set(key, obj) # we now have obj, and future passes through this code # will use the object from the cache. Detailed Documentation ====================== More detailed documentation is available in the L{Client} class. iÿÿÿÿNsEvan Martin s 1.2_tummy5s$Copyright (C) 2003 Danga InteractivetPythont_ErrorcBseZRS((t__name__t __module__(((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR:stClientcBs eZdZeZdZdZdZdZdd„Z d„Z d„Z d„Z d „Z d „Zd „Zd „Zd „Zd„Zdd„Zdd„Zdd„Zd„Zdd„Zdd„Zdd„Zd„Zd„Zd„Zdd„Zd„ZRS(sv Object representing a pool of memcache servers. See L{memcache} for an overview. In all cases where a key is used, the key can be either: 1. A simple hashable type (string, integer, etc.). 2. A tuple of C{(hashvalue, key)}. This is useful if you want to avoid making this module calculate a hash value. You may prefer, for example, to keep all of a given user's objects on the same memcache server, so you could use the user's unique id as the hash value. @group Setup: __init__, set_servers, forget_dead_hosts, disconnect_all, debuglog @group Insertion: set, add, replace @group Retrieval: get, get_multi @group Integers: incr, decr @group Removal: delete @sort: __init__, set_servers, forget_dead_hosts, disconnect_all, debuglog, set, add, replace, get, get_multi, incr, decr, delete iiii cCs#|i|ƒ||_h|_dS(sï Create a new Client object with the given list of servers. @param servers: C{servers} is passed to L{set_servers}. @param debug: whether to display error messages when a server can't be contacted. N(t set_serverstdebugtstats(tselftserversR((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt__init__Zs  cCs>g}|D]}|t||iƒq ~|_|iƒdS(sd Set the pool of servers used by this client. @param servers: an array of servers. Servers can be passed in two forms: 1. Strings of the form C{"host:port"}, which implies a default weight of 1. 2. Tuples of the form C{("host:port", weight)}, where C{weight} is an integer weight value. N(t_HosttdebuglogR t _init_buckets(RR t_[1]ts((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRfs 0cCsÙg}xÌ|iD]Á}|iƒpqnd|i|i|if}|idƒh}|i||fƒ|i}xYtoQ|ƒ}| p|i ƒdjoPn|i ddƒ}|d||d>> mc.set("counter", "20") # returns 1, indicating success 1 >>> mc.incr("counter") 21 >>> mc.incr("counter") 22 Overflow on server is not checked. Be aware of values approaching 2**32. See L{decr}. @param delta: Integer amount to increment by (should be zero or greater). @return: New value after incrementing. @rtype: int tincr(t _incrdecr(RR6tdelta((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRCÒscCs|id||ƒS(sV Like L{incr}, but decrements. Unlike L{incr}, underflow is checked and new values are capped at 0. If server value is 1, a decrement of 2 returns 0, not -1. @param delta: Integer amount to decrement by (should be zero or greater). @return: New value after decrementing. @rtype: int tdecr(RD(RR6RE((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRFës cCs™|i|ƒ\}}|pdS|i|ƒd|||f}y'|i|ƒ|iƒ}t|ƒSWn*tij o}|i|dƒdSXdS(Nis%s %s %di( R8R)RRtintR=R>R?R5(RRAR6RER.RRB((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRD÷s   cCs|id|||ƒS(s¿ Add new key with value. Like L{set}, but only stores in memcache if the key doesn't already exist. @return: Nonzero on success. @rtype: int tadd(t_set(RR6tvalR@((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRHs cCs|id|||ƒS(sÚReplace existing key with value. Like L{set}, but only stores in memcache if the key already exists. The opposite of L{add}. @return: Nonzero on success. @rtype: int treplace(RI(RR6RJR@((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRKs cCs|id|||ƒS(sÕUnconditionally sets a key to a given value in the memcache. The C{key} can optionally be an tuple, with the first element being the hash value, if you want to avoid making this module calculate a hash value. You may prefer, for example, to keep all of a given user's objects on the same memcache server, so you could use the user's unique id as the hash value. @return: Nonzero on success. @rtype: int tset(RI(RR6RJR@((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRLs c Cs<|i|ƒ\}}|pdS|i|ƒd}t|tƒon„t|tƒo|tiO}d|}nYt|tƒo|tiO}d|}n.|i o#|ti O}t i |dƒ}nd||||t |ƒ|f}y|i|ƒ|idƒWn*tij o}|i|dƒdSXdS(Nis%dis%s %s %d %d %d %stSTOREDi(R8R)R0R'RGRt _FLAG_INTEGERtlongt _FLAG_LONGt _usePicklet _FLAG_PICKLEtpickletdumpsR4RR#R=R>R?( RRAR6RJR@R.tflagstfullcmdRB((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRI's0     " cCsÚ|i|ƒ\}}|pdS|idƒy[|id|ƒ|i|ƒ\}}}|pdS|i|||ƒ}|idƒWnJtti fj o5}t |t ƒo|d}n|i |ƒdSX|S(sXRetrieves a key from the memcache. @return: The value or None. tgetsget %sRiN( R8R5R)Rt _expectvaluet _recv_valueR#RR=R>R0R1R?(RR6R.trkeyRUtrlentvalueRB((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRWFs"  c CsÕ|idƒh}x]|D]U}|i|ƒ\}}|pqn||jog||>> success = mc.set("foo", "bar") >>> success = mc.set("baz", 42) >>> mc.get_multi(["foo", "baz", "foobar"]) == {"foo": "bar", "baz": 42} 1 This method is recommended over regular L{get} as it lowers the number of total packets flying around your network, reducing total latency, since your app doesn't have to wait for each round-trip of L{get} before sending the next one. @param keys: An array of keys. @return: A dictionary of key/value pairs that were available. t get_multisget %sRiRN(R)R8RtkeysRtjoinR=R>R?RRXR5RYR( RR^t server_keysR6R.t dead_serversRBtretvalsRRZRUR[RJ((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR]_sH   "    cCsn|p|iƒ}n|d djo>|iƒ\}}}}t|ƒ}t|ƒ}|||fSdSdS(NitVALUE(NNN(RRRGR5(RR.RtrespRZRUR4R[((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRX›s  cCs!|d7}|i|ƒ}t|ƒ|jo tdt|ƒ|fƒ‚nt|ƒ|jo|d }n|djo |}nš|ti@ot|ƒ}n||ti@ot|ƒ}n^|ioB|ti @o4yt i |ƒ}Wq|i dƒd}qXn|i d|ƒ|S(Nis#received %d bytes when expecting %diþÿÿÿisPickle error... sunknown flags on get: %x (trecvR4RRRNRGRPRORQRRRStloadsR R5(RR.RUR[tbufRJ((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRY§s(     iiiN( RRt__doc__tFalseRQRRRNRPR3R RR R!R R)R+R R8R:R;RCRFRDRHRKRLRIRWR]R5RXRY(((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR=s8               < R cBsteZdZd d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „ZRS( icCsÁt|tƒo|d}|d|_n d|_|idƒdjo1|idƒ\|_|_t|iƒ|_n|d|_|_|p d„}n||_d|_ d|_ dS(Niit:iË+cSs|S(((tx((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pytÒs( R0R1RtfindRRRRGR t deaduntilR5R=(Rthostt debugfunc((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR Äs     cCs2|io|itiƒjodSd|_dS(Nii(RnR@(R((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt _check_deadØs  cCs|iƒodSdS(Nii(t _get_socket(R((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRÞs cCs;|id||fƒtiƒti|_|iƒdS(Ns MemCache: %s: %s. Marking dead.(R R@R t _DEAD_RETRYRnR9(Rtreason((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR?ãscCs—|iƒodS|io|iStititiƒ}y|i|i|ifƒWn.tij o}|i d|dƒdSX||_|S(Ns connect: %si( RqR5R=tAF_INETt SOCK_STREAMRRRR>R?(RRRB((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRrès   cCs(|io|iiƒd|_ndS(N(R=tcloseR5(R((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR9÷s  cCsOt|ƒdjo$|ii|ƒ|iidƒn|ii|dƒdS(Nids (R4R=tsendall(RRA((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRüscCsŒd}|ii}xstok|dƒ}|p|idt|ƒƒPn|djo!|o|ddjo |d S||}qW|S(Ntis'Connection closed while reading from %ss iÿÿÿÿs (R=ReRR?trepr(RtbuffersReR((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRs   % cCs8|iƒ}||jo|id||fƒn|S(Ns2while expecting '%s', got unexpected response '%s'(RR (RttextR((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR#s  cCsKd}|ii}x2t|ƒ|jo|||t|ƒƒ}qW|S(NRy(R=ReR4(RR[RgRe((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyRes  cCs8d}|iod|i}nd|i|i|fS(NRys (dead until %d)s%s:%d%s(RnRR(Rtd((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt__str__s N(RRRsR5R RqRR?RrR9RRR#ReR~(((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR Ás          cCsSddk}ddk}dg}t|ddƒ}h|d6}|i|d|ƒS(Niÿÿÿÿs127.0.0.1:11211Ritmctglobs(tdoctesttmemcacheRttestmod(RR‚R RR€((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt_doctest$s   t__main__sTesting docstrings...sRunning tests:s127.0.0.1:11211RicCs-t|tƒpd|t|ƒfSd|S(Ns%s (%s)s%s(R0R'ttype(RJ((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pytto_s3scCs^dt|ƒt|ƒfGHti||ƒti|ƒ}||jo dGHdSdGHdSdS(NsTesting set/get {'%s': %s} ...R"itFAILi(R‡RRLRW(R6RJtnewval((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt test_setget7s t FooStructcBs#eZd„Zd„Zd„ZRS(cCs d|_dS(Ntbaz(tbar(R((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR CscCsdS(Ns A FooStruct((R((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR~EscCs%t|tƒo|i|ijSdS(Ni(R0R‹R(Rtother((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt__eq__Gs(RRR R~R(((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyR‹Bs  ta_stringssome random stringt an_integeri*ROisTesting delete ...R"RˆsTesting get_multi ...sTesting get(unknown value) ...t unknown_valuet foostructsTesting incr ...i+sTesting decr ...(RhR$R=R@tcPickleRSt ImportErrort __author__t __version__t __copyright__t __license__t ExceptionRRR R„RR RR‡RŠR‹ROR;R]RWtfRCRkRF(((s</usr/lib64/python2.6/site-packages/Cheetah/Utils/memcache.pyt+s`   ÿ…c