Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c7faeee authored by Jesse Wilson's avatar Jesse Wilson
Browse files

Adopt LruCache in SettingsProvider.

Change-Id: I223ed2a4bd90234ea7e3447f19e18c68beae2763
http://b/3184897
parent 83a7b963
Loading
Loading
Loading
Loading
+10 −19
Original line number Diff line number Diff line
@@ -17,11 +17,7 @@
package com.android.providers.settings;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

@@ -47,6 +43,7 @@ import android.provider.MediaStore;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.LruCache;

public class SettingsProvider extends ContentProvider {
    private static final String TAG = "SettingsProvider";
@@ -293,7 +290,7 @@ public class SettingsProvider extends ContentProvider {
            "" + (MAX_CACHE_ENTRIES + 1) /* limit */);
        try {
            synchronized (cache) {
                cache.clear();
                cache.evictAll();
                cache.setFullyMatchesDisk(true);  // optimistic
                int rows = 0;
                while (c.moveToNext()) {
@@ -359,8 +356,8 @@ public class SettingsProvider extends ContentProvider {
    // possibly with a null value, or null on failure.
    private Bundle lookupValue(String table, SettingsCache cache, String key) {
        synchronized (cache) {
            if (cache.containsKey(key)) {
            Bundle value = cache.get(key);
            if (value != null) {
                if (value != TOO_LARGE_TO_CACHE_MARKER) {
                    return value;
                }
@@ -725,13 +722,13 @@ public class SettingsProvider extends ContentProvider {
     * associated helper functions to keep cache coherent with the
     * database.
     */
    private static final class SettingsCache extends LinkedHashMap<String, Bundle> {
    private static final class SettingsCache extends LruCache<String, Bundle> {

        private final String mCacheName;
        private boolean mCacheFullyMatchesDisk = false;  // has the whole database slurped.

        public SettingsCache(String name) {
            super(MAX_CACHE_ENTRIES, 0.75f /* load factor */, true /* access ordered */);
            super(MAX_CACHE_ENTRIES);
            mCacheName = name;
        }

@@ -751,15 +748,9 @@ public class SettingsProvider extends ContentProvider {
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry eldest) {
            if (size() <= MAX_CACHE_ENTRIES) {
                return false;
            }
            synchronized (this) {
        protected synchronized void entryEvicted(String key, Bundle value) {
            mCacheFullyMatchesDisk = false;
        }
            return true;
        }

        /**
         * Atomic cache population, conditional on size of value and if
@@ -772,7 +763,7 @@ public class SettingsProvider extends ContentProvider {
            Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
            if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
                synchronized (this) {
                    if (!containsKey(key)) {
                    if (get(key) == null) {
                        put(key, bundle);
                    }
                }
@@ -826,7 +817,7 @@ public class SettingsProvider extends ContentProvider {
                return;
            }
            synchronized (cache) {
                cache.clear();
                cache.evictAll();
                cache.mCacheFullyMatchesDisk = false;
            }
        }