def test_log_misses():
    @gen_cache.wrap("project", "project:a", "global:portal_id", timeout=3600, log_misses=True)
    def func_with_lots_of_args(a, b, foobar=None, **blakwargs):
        return time() + random.randint(0, 10000000)

    first_result = func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)

    gen_cache.invalidate("project")
    second_result = func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    ok_(first_result != second_result)
def test_gen_cache_build_key():
    key = gen_cache.build_key("unittest", "myproject", "testnum:num", "anothergeneration:gen", num=98, gen="anothergen")
    key2 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", "anothergeneration:gen", num=98, gen="anothergen"
    )
    eq_(key, key2)

    gen_cache.invalidate("myproject")
    key3 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", "anothergeneration:gen", num=98, gen="anothergen"
    )
    ok_(key3 != key2)

    gen_cache.invalidate("testnum:num", num=98)
    key4 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", "anothergeneration:gen", num=98, gen="anothergen"
    )
    ok_(key3 != key4)
def test_instance_methods():
    class BestClassEvar(object):
        @gen_cache.wrap("project", "project:a", "global:portal_id", timeout=3600)
        def func_with_lots_of_args(self, a, b, foobar=None, **blakwargs):
            return time() + random.randint(0, 10000000)

    bce = BestClassEvar()
    first_result = bce.func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    second_result = bce.func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    third_result = bce.func_with_lots_of_args("one", "NOT two", foobar="hello", portal_id=42)
    fourth_result = bce.func_with_lots_of_args("one", "two", foobar="NOT hello", portal_id=42)

    ok_(first_result == second_result)

    ok_(first_result != third_result)
    ok_(first_result != fourth_result)

    gen_cache.invalidate("project")
    fifth_result = bce.func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    ok_(first_result != fifth_result)
def test_really_long_gen_cache_build_key():
    SUPER_LONG_STRING = "lorem" * 84
    SLIGHTLY_LONG_GEN = "anothergeneration222222222222222222222222222222222222222222222222222222222222222:gen"

    key = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", SLIGHTLY_LONG_GEN, SUPER_LONG_STRING, num=98, gen="anothergen"
    )
    key2 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", SLIGHTLY_LONG_GEN, SUPER_LONG_STRING, num=98, gen="anothergen"
    )
    eq_(key, key2)

    gen_cache.invalidate("myproject")
    key3 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", SLIGHTLY_LONG_GEN, SUPER_LONG_STRING, num=98, gen="anothergen"
    )
    ok_(key3 != key2)

    gen_cache.invalidate(SUPER_LONG_STRING)
    key4 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", SLIGHTLY_LONG_GEN, SUPER_LONG_STRING, num=98, gen="anothergen"
    )
    ok_(key4 != key3)

    gen_cache.invalidate(SLIGHTLY_LONG_GEN, gen="anothergen")
    key5 = gen_cache.build_key(
        "unittest", "myproject", "testnum:num", SLIGHTLY_LONG_GEN, SUPER_LONG_STRING, num=98, gen="anothergen"
    )
    ok_(key5 != key4)
def test_genenerational_cache_invalidation():
    @gen_cache.wrap("project", "project:a", "global:portal_id", timeout=3600)
    def func_with_lots_of_args(a, b, foobar=None, **blakwargs):
        return time() + random.randint(0, 10000000)

    first_result = func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)

    gen_cache.invalidate("project")
    second_result = func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    ok_(first_result != second_result)

    gen_cache.invalidate("project:a", a="one")
    third_result = func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    ok_(second_result != third_result)

    gen_cache.invalidate("global:portal_id", portal_id=42)
    fourth_result = func_with_lots_of_args("one", "two", foobar="hello", portal_id=42)
    ok_(third_result != fourth_result)