Cached Calculator

Write a class Calculator that has the same methods as the previous one (add and subtract). But it should also have an attribute cache that keeps track of the operations being already performed as a dictionary (see example below and test cases). If the operation was already performed, the method should return the value from the cache. Example:

>>> c = Calculator()
>>> c.cache
{}
>>> c.add(2, 3)
5
>>> c.cache
{
  'add': [
    (2, 3, 5)
  ]
}
>>> c.subtract(8, 2)
6
>>> c.cache
{
  'add': [
    (2, 3, 5)
  ],
  'subtract': [
    (8, 2, 5)
  ]
}
>>> c.add(9, 3)
12
>>> c.cache
{
  'add': [
    (2, 3, 5),
    (9, 3, 12)
  ],
  'subtract': [
    (8, 2, 5)
  ]
}
# Same method invoked again:
>>> c.add(2, 3)  # Should be returned from the cache
5
import unittest


class CalculatorTestCase(unittest.TestCase):

    def test_calculator_cache(self):
        c = Calculator()
        self.assertTrue(hasattr(c, 'cache'))
        self.assertEqual(c.cache, {})

        self.assertEqual(c.add(2, 8), 10)
        self.assertEqual(c.cache, {
            'add': [
                (2, 8, 10)
            ]
        })

        self.assertEqual(c.subtract(7, 2), 5)
        self.assertEqual(c.cache, {
            'add': [
                (2, 8, 10)
            ],
            'subtract': [
                (7, 2, 5)
            ]
        })
        self.assertEqual(c.subtract(11, 7), 4)
        self.assertEqual(c.cache, {
            'add': [
                (2, 8, 10)
            ],
            'subtract': [
                (7, 2, 5),
                (11, 7, 4)
            ]
        })

        self.assertEqual(c.add(2, 8), 10)
        self.assertEqual(c.cache, {
            'add': [
                (2, 8, 10)
            ],
            'subtract': [
                (7, 2, 5),
                (11, 7, 4)
            ]
        })