@Override
    public synchronized V put(final K key, final V value) {
        checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
        checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);
        // Delete an old entry if it exists.
        final V answer = remove(key);

        int objectSize = 1;
        try {
             objectSize = CacheSizes.sizeOfAnything(value);
        }
        catch (final CannotCalculateSizeException e) {
             Log.warn(e.getMessage(), e);
        }

        // If the object is bigger than the entire cache, simply don't add it.
        if (maxCacheSize > 0 && objectSize > maxCacheSize * .90) {
            Log.warn("Cache: " + name + " -- object with key " + key +
                    " is too large to fit in cache. Size is " + objectSize);
            return answer;
        }
        cacheSize += objectSize;
        final DefaultCache.CacheObject<V> cacheObject = new DefaultCache.CacheObject<>(value, objectSize);
        map.put(key, cacheObject);
        // Make an entry into the cache order list.
        // Store the cache order list entry so that we can get back to it
        // during later lookups.
        cacheObject.lastAccessedListNode = lastAccessedList.addFirst(key);
        // Add the object to the age list
        final LinkedListNode<K> ageNode = ageList.addFirst(key);
        // We make an explicit call to currentTimeMillis() so that total accuracy
        // of lifetime calculations is better than one second.
        ageNode.timestamp = System.currentTimeMillis();
        cacheObject.ageListNode = ageNode;

        // If cache is too full, remove least used cache entries until it is
        // not too full.
        cullCache();

        return answer;
    }
Create a new default cache and specify the maximum size of for the cache in
bytes, and the maximum lifetime of objects.

@param name a name for the cache.
@param maxSize the maximum size of the cache in bytes. -1 means the cache
     has no max size.
@param maxLifetime the maximum amount of time objects can exist in
     cache before being deleted. -1 means objects never expire.
xmppserver/src/main/java/org/jivesoftware/util/cache/DefaultCache.java