<commit_before>#!/usr/bin/env python

import shelve
import time


class Cache(object):
    """
    """
    name = 'test.db'
    timeout = 10

    def __init__(self, func):
        """
        """
        self.func = func
        self.key = "%s.%s" % (func.__module__, func.func_name)
        self.cache = shelve.open(self.name)
    #__init__

    def __call__(self, *args, **kwargs):
        """
        """
        key = self.key + str(args + tuple(sorted(kwargs.items())))
        print key
        #now = int(time.time())
        #expiration = now + self.timeout

        #for key in self.cache.iterkeys():
        #    print key
        #    if self.cache[key][0] < now:
        #        print self.cache[key][0]

        if not key in self.cache:
            print "new"
            self.cache[key] = self.func(*args, **kwargs)
            self.cache.sync()
            #self.cache[key] = [expiration, self.func(*args, **kwargs)]
        #else:
        #    print "update"
        #    self.cache[key][0] = expiration

        print "cache"
        return self.cache[key]
    #__call__
#Cache

def f():
    pass

@Cache
def add(a, b, x=1, opt=0):
    return a + b + x + opt<commit_msg>Added a timeout and refresh to the cache.<commit_after>#!/usr/bin/env python

import shelve
import time

class Cache(object):
    """
    """
    name = 'test.db'
    refresh = 2
    timeout = 5

    def __init__(self, func):
        """
        """
        self.func = func
        self.key = "%s.%s" % (func.__module__, func.func_name)
        self.cache = shelve.open(self.name)
    #__init__

    def __call__(self, *args, **kwargs):
        """
        """
        now = int(time.time())
        refresh = now + self.refresh
        timeout = now + self.timeout

        # Purge timed out entries.
        for key in self.cache:
            if self.cache[key][0] < now or self.cache[key][1] < now:
                del self.cache[key]

        # Update the cache.
        key = self.key + str(args + tuple(sorted(kwargs.items())))
        if key in self.cache:       # Refresh the entry.
            entry = self.cache[key] # Use this instead of writeback.
            entry[0] = refresh
            self.cache[key] = entry
        #if
        else:                       # Add a new entry.
            print "new"
            self.cache[key] = [refresh, timeout, self.func(*args, **kwargs)]
        self.cache.sync()

        return self.cache[key][2]
    #__call__
#Cache

def f():
    pass

@Cache
def add(a, b, x=1, opt=0):
    return a + b + x + opt
{'fraction_non_alphanumeric': 0.14207221350078492, 'fraction_numerical': 0.005886970172684459, 'mean_word_length': 1.7059447983014862, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6352573', 'n_tokens_mistral': 833, 'n_tokens_neox': 809, 'n_words': 207}