Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/protocols/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyc

ó
[³XMc@s dZyddlmZWn'ek
rCdefd„ƒYZnXddlmZddlmZddl	m
Z
mZmZddl
mZd	Zd
efd„ƒYZdefd
„ƒYZdefd„ƒYZdefd„ƒYZdeefd„ƒYZddd
ddgZdS(sp
Memcache client protocol. Memcached is a caching server, storing data in the
form of pairs key/value, and memcache is the protocol to talk with it.

To connect to a server, create a factory for L{MemCacheProtocol}::

    from twisted.internet import reactor, protocol
    from twisted.protocols.memcache import MemCacheProtocol, DEFAULT_PORT
    d = protocol.ClientCreator(reactor, MemCacheProtocol
        ).connectTCP("localhost", DEFAULT_PORT)
    def doSomething(proto):
        # Here you call the memcache operations
        return proto.set("mykey", "a lot of data")
    d.addCallback(doSomething)
    reactor.run()

All the operations of the memcache protocol are present, but
L{MemCacheProtocol.set} and L{MemCacheProtocol.get} are the more important.

See U{http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt} for
more information about the protocol.
iÿÿÿÿ(tdequeRcBseZd„ZRS(cCs
|jdƒS(Ni(tpop(tself((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytpopleft s(t__name__t
__module__R(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRs(tLineReceiver(tTimeoutMixin(tDeferredtfailtTimeoutError(tlogiË+t
NoSuchCommandcBseZdZRS(sA
    Exception raised when a non existent command is called.
    (RRt__doc__(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR/stClientErrorcBseZdZRS(s1
    Error caused by an invalid client call.
    (RRR
(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR6stServerErrorcBseZdZRS(s*
    Problem happening on the server.
    (RRR
(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR=stCommandcBs)eZdZd„Zd„Zd„ZRS(s6
    Wrap a client action into an object, that holds the values used in the
    protocol.

    @ivar _deferred: the L{Deferred} object that will be fired when the result
        arrives.
    @type _deferred: L{Deferred}

    @ivar command: name of the command sent to the server.
    @type command: C{str}
    cKsF||_tƒ|_x*|jƒD]\}}t|||ƒq"WdS(sÝ
        Create a command.

        @param command: the name of the command.
        @type command: C{str}

        @param kwargs: this values will be stored as attributes of the object
            for future use
        N(tcommandRt	_deferredtitemstsetattr(RRtkwargstktv((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt__init__Qs
	cCs|jj|ƒdS(sB
        Shortcut method to fire the underlying deferred.
        N(Rtcallback(Rtvalue((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytsuccessascCs|jj|ƒdS(s5
        Make the underlying deferred fails.
        N(Rterrback(Rterror((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR	hs(RRR
RRR	(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRDs		tMemCacheProtocolcBs‘eZdZdZeZdd„Zd„Zd„Zd„Z	d„Z
d„Zd	„Zd
„Z
d„Zd„Zd
„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zdd„Zdd„Zd„Zddd„Zddd„Zddd„Zddd„Z d „Z!d!„Z"d"„Z#ed#„Z$ed$„Z%d%„Z&d*d&„Z(d'„Z)d(„Z*d)„Z+RS(+s1
    MemCache protocol: connect to a memcached server to store/retrieve values.

    @ivar persistentTimeOut: the timeout period used to wait for a response.
    @type persistentTimeOut: C{int}

    @ivar _current: current list of requests waiting for an answer from the
        server.
    @type _current: C{deque} of L{Command}

    @ivar _lenExpected: amount of data expected in raw mode, when reading for
        a value.
    @type _lenExpected: C{int}

    @ivar _getBuffer: current buffer of data, used to store temporary data
        when reading in raw mode.
    @type _getBuffer: C{list}

    @ivar _bufferLength: the total amount of bytes in C{_getBuffer}.
    @type _bufferLength: C{int}

    @ivar _disconnected: indicate if the connectionLost has been called or not.
    @type _disconnected: C{bool}
    iúi<cCs;tƒ|_d|_d|_d|_||_|_dS(sÓ
        Create the protocol.

        @param timeOut: the timeout to wait before detecting that the
            connection is dead and close it. It's expressed in seconds.
        @type timeOut: C{int}
        N(Rt_currenttNonet_lenExpectedt
_getBuffert
_bufferLengthtpersistentTimeOutttimeOut(RR%((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRŒs
			cCs0x)|jr+|jjƒ}|j|ƒqWdS(sW
        Cancel all the outstanding commands, making them fail with C{reason}.
        N(RRR	(Rtreasontcmd((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt_cancelCommands›scCs$|jtdƒƒ|jjƒdS(s:
        Close the connection in case of timeout.
        sConnection timeoutN(R(R
t	transporttloseConnection(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyttimeoutConnection¤scCs*t|_|j|ƒtj||ƒdS(s9
        Cause any outstanding commands to fail.
        N(tTruet
_disconnectedR(RtconnectionLost(RR&((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR.¬s	
cCs0|js|j|jƒntj||ƒdS(sA
        Override sendLine to add a timeout to response.
        N(Rt
setTimeoutR$RtsendLine(Rtline((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR0µs	cCsû|jƒ|jj|ƒ|jt|ƒ7_|j|jdkr÷dj|jƒ}||j }||jd}|}d|_d|_d|_|jd}|j	rÞ|j
|j\}}|||f|j
|j<n	||_|j
|ƒndS(s)
        Collect data for a get.
        itiN(tresetTimeoutR"tappendR#tlenR!tjoinR Rtmultipletvaluest
currentKeyRtsetLineMode(RtdatatbuftremtvalR'tflagstcas((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytrawDataReceived¾s"

			
		cCs|jjƒjtƒdS(s?
        Manage a success response to a set operation.
        N(RRRR,(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt
cmd_STOREDÖscCs|jjƒjtƒdS(sŠ
        Manage a specific 'not stored' response to a set operation: this is not
        an error, but some condition wasn't met.
        N(RRRtFalse(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_NOT_STOREDÝscCs|jjƒ}|jdkr”|jrxtg|jjƒD]%\}}||ddd…f^q:ƒ}|j|ƒq|j|j|j	fƒno|jdkrá|jr¿|j|jƒq|j|j|j
|j	fƒn"|jdkr|j|jƒndS(sB
        This the end token to a get or a stat operation.
        tgetNitgetststats(RRRR7tdictR8t	iteritemsRR?RR@(RR'tkeyR>R8((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_ENDås	;	"cCs|jjƒjtƒdS(s=
        Manage error response for incr/decr/delete.
        N(RRRRC(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt
cmd_NOT_FOUNDúscCs|jd}|jdkr:|jƒ\}}}d}n|jƒ\}}}}t|ƒ|_g|_d|_|jr¿||jkršt	dƒ‚n||_
t|ƒ|g|j|<n6|j|krÝt	dƒ‚nt|ƒ|_
||_|jƒdS(s:
        Prepare the reading a value after a get.
        iRER2sUnexpected commands answer.N(RRtsplittintR!R"R#R7tkeystRuntimeErrorR9R8RJR?R@t
setRawMode(RR1R'RJR?tlengthR@((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt	cmd_VALUEs$
						cCs6|jd}|jddƒ\}}||j|<dS(s-
        Reception of one stat line.
        it iN(RRMR8(RR1R'RJR>((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_STATs
cCs|jjƒj|ƒdS(s%
        Read version token.
        N(RRR(RtversionData((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_VERSION$scCs0tjdƒ|jjƒ}|jtƒƒdS(s8
        An non-existent command has been sent.
        sNon-existent command sent.N(RterrRRR	R(RR'((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt	cmd_ERROR+s
cCs:tjd|fƒ|jjƒ}|jt|ƒƒdS(s0
        An invalid input as been sent.
        sInvalid input: %sN(RRXRRR	R(RterrTextR'((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_CLIENT_ERROR4scCs:tjd|fƒ|jjƒ}|jt|ƒƒdS(s4
        An error has happened server-side.
        sServer error: %sN(RRXRRR	R(RRZR'((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_SERVER_ERROR=scCs|jjƒjtƒdS(s>
        A delete command has completed successfully.
        N(RRRR,(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_DELETEDFscCs|jjƒjtƒdS(s6
        The last command has been completed.
        N(RRRR,(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcmd_OKMscCs|jjƒjtƒdS(s5
        A C{checkAndSet} update has failed.
        N(RRRRC(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt
cmd_EXISTSTscCs|jƒ|jddƒd}t|d|fdƒ}|dk	r||jddƒd}|rr||dƒqå|ƒni|jddƒ}t|d|fdƒ}|dk	r½|ƒn(|jjƒ}t|ƒ}|j|ƒ|jsþ|j	dƒndS(s8
        Receive line commands from the server.
        RTiiscmd_%st_N(
R3RMtgetattrR treplaceRRRNRR/(RR1ttokenR'targsR>((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytlineReceived[s"



	icCs|jd||ƒS(s¼
        Increment the value of C{key} by given value (default to 1).
        C{key} must be consistent with an int. Return the new value.

        @param key: the key to modify.
        @type key: C{str}

        @param val: the value to increment.
        @type val: C{int}

        @return: a deferred with will be called back with the new value
            associated with the key (after the increment).
        @rtype: L{Deferred}
        tincr(t	_incrdecr(RRJR>((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt	incrementyscCs|jd||ƒS(sÞ
        Decrement the value of C{key} by given value (default to 1).
        C{key} must be consistent with an int. Return the new value, coerced to
        0 if negative.

        @param key: the key to modify.
        @type key: C{str}

        @param val: the value to decrement.
        @type val: C{int}

        @return: a deferred with will be called back with the new value
            associated with the key (after the decrement).
        @rtype: L{Deferred}
        tdecr(Rg(RRJR>((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt	decrement‹scCs¹|jrttdƒƒSt|tƒsEttdt|ƒfƒƒSt|ƒ|jkrjttdƒƒSd||t	|ƒf}|j
|ƒt|d|ƒ}|jj
|ƒ|jS(s1
        Internal wrapper for incr/decr.
        s
not connecteds,Invalid type for key: %s, expecting a stringsKey too longs%s %s %dRJ(R-R	RPt
isinstancetstrRttypeR5tMAX_KEY_LENGTHRNR0RRR4R(RR'RJR>tfullcmdtcmdObj((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRgžs	
icCs|jd||||dƒS(s™
        Replace the given C{key}. It must already exist in the server.

        @param key: the key to replace.
        @type key: C{str}

        @param val: the new value associated with the key.
        @type val: C{str}

        @param flags: the flags to store with the key.
        @type flags: C{int}

        @param expireTime: if different from 0, the relative time in seconds
            when the key will be deleted from the store.
        @type expireTime: C{int}

        @return: a deferred that will fire with C{True} if the operation has
            succeeded, and C{False} with the key didn't previously exist.
        @rtype: L{Deferred}
        RbR2(t_set(RRJR>R?t
expireTime((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRb°scCs|jd||||dƒS(s€
        Add the given C{key}. It must not exist in the server.

        @param key: the key to add.
        @type key: C{str}

        @param val: the value associated with the key.
        @type val: C{str}

        @param flags: the flags to store with the key.
        @type flags: C{int}

        @param expireTime: if different from 0, the relative time in seconds
            when the key will be deleted from the store.
        @type expireTime: C{int}

        @return: a deferred that will fire with C{True} if the operation has
            succeeded, and C{False} with the key already exists.
        @rtype: L{Deferred}
        taddR2(Rq(RRJR>R?Rr((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRsÈscCs|jd||||dƒS(s5
        Set the given C{key}.

        @param key: the key to set.
        @type key: C{str}

        @param val: the value associated with the key.
        @type val: C{str}

        @param flags: the flags to store with the key.
        @type flags: C{int}

        @param expireTime: if different from 0, the relative time in seconds
            when the key will be deleted from the store.
        @type expireTime: C{int}

        @return: a deferred that will fire with C{True} if the operation has
            succeeded.
        @rtype: L{Deferred}
        tsetR2(Rq(RRJR>R?Rr((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRtàscCs|jd|||||ƒS(sg
        Change the content of C{key} only if the C{cas} value matches the
        current one associated with the key. Use this to store a value which
        hasn't been modified since last time you fetched it.

        @param key: The key to set.
        @type key: C{str}

        @param val: The value associated with the key.
        @type val: C{str}

        @param cas: Unique 64-bit value returned by previous call of C{get}.
        @type cas: C{str}

        @param flags: The flags to store with the key.
        @type flags: C{int}

        @param expireTime: If different from 0, the relative time in seconds
            when the key will be deleted from the store.
        @type expireTime: C{int}

        @return: A deferred that will fire with C{True} if the operation has
            succeeded, C{False} otherwise.
        @rtype: L{Deferred}
        R@(Rq(RRJR>R@R?Rr((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytcheckAndSetøsc
Cs |jrttdƒƒSt|tƒsEttdt|ƒfƒƒSt|ƒ|jkrjttdƒƒSt|tƒs–ttdt|ƒfƒƒS|r©d|}nt|ƒ}d||||||f}|j	|ƒ|j	|ƒt
|d|d|d	|ƒ}	|jj|	ƒ|	j
S(
s6
        Internal wrapper for setting values.
        s
not connecteds,Invalid type for key: %s, expecting a stringsKey too longs.Invalid type for value: %s, expecting a stringRTs%s %s %d %d %d%sRJR?RR(R-R	RPRkRlRRmR5RnR0RRR4R(
RR'RJR>R?RrR@RRRoRp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRqs*	


cCs|jd||dddƒS(s˜
        Append given data to the value of an existing key.

        @param key: The key to modify.
        @type key: C{str}

        @param val: The value to append to the current value associated with
            the key.
        @type val: C{str}

        @return: A deferred that will fire with C{True} if the operation has
            succeeded, C{False} otherwise.
        @rtype: L{Deferred}
        R4iR2(Rq(RRJR>((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR40scCs|jd||dddƒS(sš
        Prepend given data to the value of an existing key.

        @param key: The key to modify.
        @type key: C{str}

        @param val: The value to prepend to the current value associated with
            the key.
        @type val: C{str}

        @return: A deferred that will fire with C{True} if the operation has
            succeeded, C{False} otherwise.
        @rtype: L{Deferred}
        tprependiR2(Rq(RRJR>((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRvCscCs|j|g|tƒS(s¼
        Get the given C{key}. It doesn't support multiple keys. If
        C{withIdentifier} is set to C{True}, the command issued is a C{gets},
        that will return the current identifier associated with the value. This
        identifier has to be used when issuing C{checkAndSet} update later,
        using the corresponding method.

        @param key: The key to retrieve.
        @type key: C{str}

        @param withIdentifier: If set to C{True}, retrieve the current
            identifier along with the value and the flags.
        @type withIdentifier: C{bool}

        @return: A deferred that will fire with the tuple (flags, value) if
            C{withIdentifier} is C{False}, or (flags, cas identifier, value)
            if C{True}.  If the server indicates there is no value
            associated with C{key}, the returned value will be C{None} and
            the returned flags will be C{0}.
        @rtype: L{Deferred}
        (t_getRC(RRJtwithIdentifier((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyREVscCs|j||tƒS(s
        Get the given list of C{keys}.  If C{withIdentifier} is set to C{True},
        the command issued is a C{gets}, that will return the identifiers
        associated with each values. This identifier has to be used when
        issuing C{checkAndSet} update later, using the corresponding method.

        @param keys: The keys to retrieve.
        @type keys: C{list} of C{str}

        @param withIdentifier: If set to C{True}, retrieve the identifiers
            along with the values and the flags.
        @type withIdentifier: C{bool}

        @return: A deferred that will fire with a dictionary with the elements
            of C{keys} as keys and the tuples (flags, value) as values if
            C{withIdentifier} is C{False}, or (flags, cas identifier, value) if
            C{True}.  If the server indicates there is no value associated with
            C{key}, the returned values will be C{None} and the returned flags
            will be C{0}.
        @rtype: L{Deferred}

        @since: 9.0
        (RwR,(RRORx((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytgetMultipleosc	CsG|jrttdƒƒSx_|D]W}t|tƒsRttdt|ƒfƒƒSt|ƒ|jkr ttdƒƒSq W|rŠd}nd}d|dj	|ƒf}|j
|ƒ|rtg|D]}|df^qƃ}t
|d
|d|dtƒ}n.t
|d
|ddddddd	dtƒ}|jj|ƒ|jS(s>
        Helper method for C{get} and C{getMultiple}.
        s
not connecteds,Invalid type for key: %s, expecting a stringsKey too longRFREs%s %sRTiR2ROR8R7RJRR?R@N(iR2N(R-R	RPRkRlRRmR5RnR6R0RHR RR,RCRR4R(	RRORxR7RJR'RoR8Rp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRw‰s(	
	
%!%	cCsh|rd|}nd}|jr2ttdƒƒS|j|ƒtddiƒ}|jj|ƒ|jS(sï
        Get some stats from the server. It will be available as a dict.

        @param arg: An optional additional string which will be sent along
            with the I{stats} command.  The interpretation of this value by
            the server is left undefined by the memcache protocol
            specification.
        @type arg: L{NoneType} or L{str}

        @return: a deferred that will fire with a C{dict} of the available
            statistics.
        @rtype: L{Deferred}
        sstats RGs
not connectedR8(R-R	RPR0RRR4R(RtargR'Rp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRG¤s
	
cCsI|jrttdƒƒS|jdƒtdƒ}|jj|ƒ|jS(sª
        Get the version of the server.

        @return: a deferred that will fire with the string value of the
            version.
        @rtype: L{Deferred}
        s
not connectedtversion(R-R	RPR0RRR4R(RRp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR{¾s	
cCs|jrttdƒƒSt|tƒsEttdt|ƒfƒƒS|jd|ƒtdd|ƒ}|j	j
|ƒ|jS(s
        Delete an existing C{key}.

        @param key: the key to delete.
        @type key: C{str}

        @return: a deferred that will be called back with C{True} if the key
            was successfully deleted, or C{False} if not.
        @rtype: L{Deferred}
        s
not connecteds,Invalid type for key: %s, expecting a strings	delete %stdeleteRJ(R-R	RPRkRlRRmR0RRR4R(RRJRp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyR|Îs	cCsI|jrttdƒƒS|jdƒtdƒ}|jj|ƒ|jS(s·
        Flush all cached values.

        @return: a deferred that will be called back with C{True} when the
            operation has succeeded.
        @rtype: L{Deferred}
        s
not connectedt	flush_all(R-R	RPR0RRR4R(RRp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pytflushAlläs	
N(,RRR
RnRCR-RR(R+R.R0RARBRDRKRLRSRURWRYR[R\R]R^R_ReRhRjRgRbRsRtRuRqR4RvRERyRwR RGR{R|R~(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyRpsP																																	tDEFAULT_PORTN(R
tcollectionsRtImportErrortlistttwisted.protocols.basicRttwisted.protocols.policiesRttwisted.internet.deferRR	R
ttwisted.pythonRRt	ExceptionRRRtobjectRRt__all__(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/memcache.pyt<module>s$
,ÿÿ‡