Code example for ConcurrentMap

Methods: get, put

0
     */ 
    @Override 
    @SuppressWarnings("unchecked") 
    public synchronized ValT get(Object elementKey) {
        KeyT key = (KeyT) elementKey;
        ValT value = cache.get(key);
        if (value != null) {
            // memory hit 
            Log.d(name, "MEM cache hit for " + key.toString());
            return value;
        } 
 
        // memory miss, try reading from disk 
        File file = getFileForKey(key);
        if (file.exists()) {
            // if file older than expirationInMinutes, remove it 
            long lastModified = file.lastModified();
            Date now = new Date();
            long ageInMinutes = ((now.getTime() - lastModified) / (1000 * 60));
 
            if (ageInMinutes >= expirationInMinutes) {
                Log.d(name, "DISK cache expiration for file " + file.toString());
                file.delete();
                return null; 
            } 
 
            // disk hit 
            Log.d(name, "DISK cache hit for " + key.toString());
            try { 
                value = readValueFromDisk(file);
            } catch (IOException e) {
                // treat decoding errors as a cache miss 
                e.printStackTrace();
                return null; 
            } 
            if (value == null) {
                return null; 
            } 
            cache.put(key, value);
            return value;
        } 
 
        // cache miss 
        return null;