#!/usr/bin/env python3
# encoding: utf-8
"""Least-recently-used cache
"""

# end_pymotw_header
import functools


@functools.lru_cache(maxsize=2)
def expensive(a, b):
    print("called expensive({}, {})".format(a, b))
    return a * b


def make_call(a, b):
    print("({}, {})".format(a, b), end=" ")
    pre_hits = expensive.cache_info().hits
    expensive(a, b)
    post_hits = expensive.cache_info().hits
    if post_hits > pre_hits:
        print("cache hit")


make_call(1, 2)

try:
    make_call([1], 2)
except TypeError as err:
    print("ERROR: {}".format(err))

try:
    make_call(1, {"2": "two"})
except TypeError as err:
    print("ERROR: {}".format(err))

#!/usr/bin/env python3
# encoding: utf-8
"""Least-recently-used cache
"""

# end_pymotw_header
import functools


@functools.lru_cache(maxsize=2)
def expensive(a, b):
    print("called expensive({}, {})".format(a, b))
    return a * b


def make_call(a, b):
    print("({}, {})".format(a, b), end=" ")
    pre_hits = expensive.cache_info().hits
    expensive(a, b)
    post_hits = expensive.cache_info().hits
    if post_hits > pre_hits:
        print("cache hit")


make_call(1, 2)

try:
    make_call([1], 2)
except TypeError as err:
    print("ERROR: {}".format(err))

try:
    make_call(1, {"2": "two"})
except TypeError as err:
    print("ERROR: {}".format(err))

#!/usr/bin/env python3 # encoding: utf-8 Least-recently-used cache # end_pymotw_header