Android 缓存策略demo

packageinstaller\permission\model\PermissionApps.java

 

    /**
     * Class used to reduce the number of calls to the package manager.
     * This caches app information so it should only be used across parallel PermissionApps
     * instances, and should not be retained across UI refresh.
     */
    public static class PmCache {
        private final SparseArray<List<PackageInfo>> mPackageInfoCache = new SparseArray<>();
        private final PackageManager mPm;

        public PmCache(PackageManager pm) {
            mPm = pm;
        }

        public synchronized List<PackageInfo> getPackages(int userId) {
            List<PackageInfo> ret = mPackageInfoCache.get(userId);
            if (ret == null) {
                ret = mPm.getInstalledPackagesAsUser(PackageManager.GET_PERMISSIONS, userId);
                mPackageInfoCache.put(userId, ret);
            }
            return ret;
        }
    }

 

private final PmCache mCache;

List<PackageInfo> apps = mCache != null ? mCache.getPackages(user.getIdentifier())
                    : mPm.getInstalledPackagesAsUser(PackageManager.GET_PERMISSIONS,
                            user.getIdentifier());

 

/**
* Class used to reduce the number of calls to the package manager.
* This caches app information so it should only be used across parallel PermissionApps
* instances, and should not be retained across UI refresh.
*/
public static class PmCache {
private final SparseArray<List<PackageInfo>> mPackageInfoCache = new SparseArray<>();
private final PackageManager mPm;

public PmCache(PackageManager pm) {
mPm = pm;
}

public synchronized List<PackageInfo> getPackages(int userId) {
List<PackageInfo> ret = mPackageInfoCache.get(userId);
if (ret == null) {
ret = mPm.getInstalledPackagesAsUser(PackageManager.GET_PERMISSIONS, userId);
mPackageInfoCache.put(userId, ret);
}
return ret;
}
}
posted @ 2018-07-15 11:00  行走的思想  阅读(336)  评论(0)    收藏  举报