(maxsize=100)
return decorating_function
A very simple cache that, when the cache is filled, deletes 10% of the
    cached values AT RANDOM.

    This function duplicates (more-or-less) the protocol of the
    ``functools.lru_cache`` decorator in the Python 3.2 standard library.

    Arguments to the cached function must be hashable.

    View the cache statistics tuple ``(hits, misses, maxsize, currsize)``
    with f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.
A very simple cache that, when the cache is filled, deletes 10% of the
    cached values AT RANDOM.
A very simple cache that when the cache is filled deletes 10% of the cached values AT RANDOM .
def random_cache(maxsize=100):
    """A very simple cache that, when the cache is filled, deletes 10% of the
    cached values AT RANDOM.

    This function duplicates (more-or-less) the protocol of the
    ``functools.lru_cache`` decorator in the Python 3.2 standard library.

    Arguments to the cached function must be hashable.

    View the cache statistics tuple ``(hits, misses, maxsize, currsize)``
    with f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.
    """

    def decorating_function(user_function):
        stats = [0, 0]  # hits, misses
        data = {}

        @functools.wraps(user_function)
        def wrapper(*args):
            try:
                result = data[args]
                stats[0] += 1  # Hit
            except KeyError:
                stats[1] += 1  # Miss
                if len(data) == maxsize:
                    keys = data.keys()
                    for i in xrange(maxsize // 10 or 1):
                        n = random.randint(0, len(keys) - 1)
                        k = keys.pop(n)
                        del data[k]
                data[args] = user_function(*args)
                result = data[args]
            return result

        def cache_info():
            return stats[0], stats[1], maxsize, len(data)

        def cache_clear():
            data.clear()

        wrapper.cache_info = cache_info
        wrapper.cache_clear = cache_clear
        return wrapper
    return decorating_function
def random_cache ( maxsize = 100 ) : def decorating_function ( user_function ) : stats = [ 0 , 0 ] # hits, misses data = { } @ functools . wraps ( user_function ) def wrapper ( * args ) : try : result = data [ args ] stats [ 0 ] += 1 # Hit except KeyError : stats [ 1 ] += 1 # Miss if len ( data ) == maxsize : keys = data . keys ( ) for i in xrange ( maxsize // 10 or 1 ) : n = random . randint ( 0 , len ( keys ) - 1 ) k = keys . pop ( n ) del data [ k ] data [ args ] = user_function ( * args ) result = data [ args ] return result def cache_info ( ) : return stats [ 0 ] , stats [ 1 ] , maxsize , len ( data ) def cache_clear ( ) : data . clear ( ) wrapper . cache_info = cache_info wrapper . cache_clear = cache_clear return wrapper return decorating_function