#!/usr/bin/env python

from Extracto import Cache

#cache = Cache.SizedDictCache(5, attrition=0.4)
from Extracto import config
#cache = Cache.SizedDBCache(config.db.jchangdb, "cache_sized_test",
#                      5, attrition=0.4)
cache = Cache.DBCache(config.db.jchangdb, "cache_unsized_test")

cache["hello"] = 1
print cache["hello"]    # 1
print len(cache)        # 1

cache["world"] = 2
cache["foo"] = 'five'
print len(cache)        # 3

cache["bye"] = 5
cache["bar"] = 'bee'
cache["hello"] = "one"
print len(cache)        # 5

cache["baz"] = 10
print len(cache)        # 3 (sized), 6 (unsized)
k = cache.keys()
k.sort()
print k                 # ["bar", "baz", "hello"]  (unsized will have more)

print cache.get("doesnotexist", "good catch")  # good catch

if isinstance(cache, Cache.SizedCache):
    # Test the priorities.
    print cache._min_priority(), cache._max_priority()   # 5, 7
    print cache["hello"]          # one
    print cache._max_priority()   # 8

    cache._renumber_priorities()
    print cache._min_priority(), cache._max_priority()   # 0, 2

    # Make sure DictCache's max_priority code handles the deleting top
    # priority case properly.
    del cache[cache._get_by_priority(cache._max_priority())]
    print cache._max_priority()                          # 1

cache.clear()
print len(cache)             # 0


