Code example for File

Methods: delete, lastModified, toString

0
            e.printStackTrace();
        } 
    } 
 
    private File getFileForKey(KeyT key) {
        return new File(diskCacheDirectory + "/" + getFileNameForKey(key));
    } 
 
    /** 
     * Reads a value from the cache by first probing the in-memory cache. If not found, the the disk 
     * cache will be probed. If it's a hit, the entry is written back to memory and returned. 
     *  
     * @param elementKey 
     *            the cache key 
     * @return the cached value, or null if element was not cached 
     */ 
    @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; 
            }