Android Open Source - savemypics C File Cache






From Project

Back to project page savemypics.

License

The source code is released under:

Copyright (c) 2014, KB Sriram All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. R...

If you think the Android project savemypics listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.savemypics.android.util;
//from  w  ww.  j a va2s  .  co  m
/**
 * maintain a small cache of files - this is mostly to hold resized
 * images for larger downloaded images.
 */

import android.content.Context;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

public class CFileCache
{
    @SuppressWarnings("serial")
    public CFileCache(Context ctx)
    {
        m_cacheroot = new File(ctx.getCacheDir(), "tn");
        m_access_cache = new LinkedHashMap<String,Integer>() {
            @Override
            protected boolean removeEldestEntry(Map.Entry<String,Integer> e) {
                if (size() < MAX_ENTRIES) { return false; }
                CFileCache.this.setNeedsCleanup(true);
                return true;
            }
        };
    }

    public File getFile(String key, int width, int height)
    {
        String fname = CUtils.sha(key);
        synchronized (m_access_cache) {
            File parent = new File(m_cacheroot, ""+width+"x"+height);
            if (!parent.exists()) { parent.mkdirs(); }
            File ret = new File(parent, fname);
            String access_key = ret.toString();
            if (m_access_cache.get(access_key) == null) {
                // add to accessed cache, and cleanup if necessary.
                m_access_cache.put(access_key, Integer.valueOf(0));
                maybeCleanup(parent);
            }
            return ret;
        }
    }

    private void maybeCleanup(File dir)
    {
        // Don't bother cleaning up unless we really mean it.
        if (!needsCleanup()) { return; }
        long now = System.currentTimeMillis();
        if ((now - m_last_cleanup) < MIN_CLEANUP_INTERVAL_MSEC) {
            setNeedsCleanup(false);
            return;
        }

        // Walk the cache tree; deleting anything that we don't find
        // in our access cache. This also has the side-effect of
        // randomizing the access timestamps, sigh.
        cleanupRoot(dir);
        m_last_cleanup = System.currentTimeMillis();
        setNeedsCleanup(false);
    }

    private void cleanupRoot(File dir)
    {
        if (!dir.isDirectory()) { return; }
        File[] children = dir.listFiles();
        for (int i=0; i<children.length; i++) {
            File child = children[i];
            if ((child != null) &&
                child.isFile() &&
                (m_access_cache.get(child.toString()) == null)) {
                CUtils.LOGD(TAG, "delete: "+child);
                child.delete();
            }
        }
    }

    private void setNeedsCleanup(boolean v)
    { m_cleanup = v; }
    private boolean needsCleanup()
    { return m_cleanup; }

    private final File m_cacheroot;
    private final LinkedHashMap<String,Integer> m_access_cache;
    private boolean m_cleanup = false;
    private long m_last_cleanup = 0l;
    private final static long MIN_CLEANUP_INTERVAL_MSEC = 300l*1000l;
    private final static int MAX_ENTRIES = 80;
    private final static String TAG = CUtils.makeLogTag(CFileCache.class);
}




Java Source Code List

android.util.Base64.java
org.savemypics.android.activity.AActivity.java
org.savemypics.android.activity.ABaseSettingsActivity.java
org.savemypics.android.activity.CAccountInfoListAdapter.java
org.savemypics.android.activity.CFlickrSettingsActivity.java
org.savemypics.android.activity.CSnapfishSettingsActivity.java
org.savemypics.android.activity.CStartActivity.java
org.savemypics.android.authenticator.AAuthenticatorActivity.java
org.savemypics.android.authenticator.CAuthenticationService.java
org.savemypics.android.authenticator.CAuthenticator.java
org.savemypics.android.authenticator.CFlickrLoginHookActivity.java
org.savemypics.android.authenticator.CFlickrWebActivity.java
org.savemypics.android.authenticator.CSelectProviderActivity.java
org.savemypics.android.authenticator.CSnapfishLoginActivity.java
org.savemypics.android.db.CAccount.java
org.savemypics.android.db.CDb.java
org.savemypics.android.db.CLocalImage.java
org.savemypics.android.db.CMap.java
org.savemypics.android.db.CRemoteImage.java
org.savemypics.android.event.AEvent.java
org.savemypics.android.event.CEventBus.java
org.savemypics.android.event.CExceptionEvent.java
org.savemypics.android.glue.ABaseGlue.java
org.savemypics.android.glue.CFlickrGlue.java
org.savemypics.android.glue.CSnapfishGlue.java
org.savemypics.android.receiver.CNetworkChangeReceiver.java
org.savemypics.android.service.CTaskQueue.java
org.savemypics.android.sync.CMediaUtils.java
org.savemypics.android.sync.CSyncAdapter.java
org.savemypics.android.sync.CSyncService.java
org.savemypics.android.sync.CSyncUtils.java
org.savemypics.android.util.CBitmapUtils.java
org.savemypics.android.util.CFileCache.java
org.savemypics.android.util.CLruCache.java
org.savemypics.android.util.CUtils.java
org.savemypics.android.util.CUtils.java
org.savemypics.android.view.CMaximumWidthLayout.java
org.savemypics.android.view.CModularLayout.java
org.savemypics.android.view.CRefreshButton.java
org.savemypics.android.view.CRoundedBitmapView.java
org.savemypics.android.view.CStatusCounterView.java
org.savemypics.plugin.CIOUtils.java
org.savemypics.plugin.CPlugin.java
org.savemypics.plugin.flickr.CFlickrAlbum.java
org.savemypics.plugin.flickr.CFlickrUtils.java
org.savemypics.plugin.snapfish.CSnapfishAlbum.java
org.savemypics.plugin.snapfish.CSnapfishFeed.java
org.savemypics.plugin.snapfish.CSnapfishUser.java
org.savemypics.plugin.snapfish.CSnapfishUtils.java