Fix a bug in the shared cache implementation.

We could decrement an unsigned number into negative. Also add
a test case and some better documentation to the source code.