自己封装的 —— 三级缓存

本文介绍了一个自定义的图片加载框架实现细节,包括内存缓存、SD卡缓存及网络加载等多级缓存机制,并利用Handler机制更新UI,确保主线程不被阻塞。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局

<ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

2:自己定义个工具类

public class BitmapUtils {
    Context context;
    //图片本地缓存路径
    private final static String SDCARD_CACHE = Environment.getExternalStorageDirectory() + "/imagecache";
    //图片存放文件夹
    File fileDir = new File(SDCARD_CACHE);

    private Map<String, SoftReference<Bitmap>> map = new HashMap<String, SoftReference<Bitmap>>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    ImageViewToBitmap imageViewToBitmap = (ImageViewToBitmap) msg.obj;
                    imageViewToBitmap.iv.setImageBitmap(imageViewToBitmap.bitmap);
                    break;
            }
        }
    };

    //构造方法
    public BitmapUtils(Context context) {
        this.context = context;
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
    }

    //加载图片的方法
    public void display(ImageView iv, String url) {
        //内存缓存

        Bitmap bitmap = loadMemory(url);
        if (bitmap != null) {
            iv.setImageBitmap(bitmap);
        } else {
            //sdcard缓存
            bitmap = loadSD(url);
            if (bitmap != null) {
                iv.setImageBitmap(bitmap);
            } else {
                //网络缓存
                loadInternetImage(iv, url);
            }
        }


    }

    private Bitmap loadMemory(String url) {
        SoftReference<Bitmap> value = map.get(url);
        if (value != null) {
            Bitmap bitmap = value.get();
            return bitmap;
        }
        return null;

    }

    //获取本地图片
    private Bitmap loadSD(String url) {
        String name = getFileName(url);
        File file = new File(fileDir, name);
        if (file.exists()) {
            //BitmapFactory选项
            BitmapFactory.Options options = new BitmapFactory.Options();
            //加载图片宽高
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(name, options);
            //获取图片和手机屏幕宽高
            int outWidth = options.outWidth;
            int outHeight = options.outHeight;
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            int widthPixels = displayMetrics.widthPixels;
            int heightPixels = displayMetrics.heightPixels;
            //图片跟手机屏幕进行比对
            int scale = 0;
            int scaleX = outWidth / widthPixels;
            int scaleY = outHeight / heightPixels;
            scale = scaleX > scaleY ? scaleX : scaleY;
            if (scale == 0) {
                scale = 1;
            }
            options.inJustDecodeBounds = false;
            options.inSampleSize = scale;
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            //内存缓存
            SoftReference<Bitmap> value = new SoftReference<Bitmap>(bitmap);
            map.put(name, value);
            return bitmap;
        }


        return null;

    }

    //获取网络图片
    private void loadInternetImage(ImageView iv, String url) {
        //开子线程做耗时操作
        new Thread(new DownloadImage(iv, url)).start();

    }

    private class DownloadImage implements Runnable {
        ImageView iv;
        String url;
        private InputStream inputStream;
        private FileOutputStream fos;

        public DownloadImage(ImageView iv, String url) {
            this.iv = iv;
            this.url = url;
        }

        @Override
        public void run() {
            try {
            HttpClient clent = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);

                HttpResponse execute = clent.execute(get);
                if (execute.getStatusLine().getStatusCode() == 200) {
                    inputStream = execute.getEntity().getContent();
                    String name = getFileName(url);
                    File file = new File(fileDir, name);
                    fos = new FileOutputStream(file);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }

                    //sdcard缓存
                    Bitmap bitmap = loadSD(name);
                    //ImageView转换成Bitmap转换成Bitmap
                    ImageViewToBitmap ivtb = new ImageViewToBitmap(iv, bitmap);
                    Message message = Message.obtain(handler, 0, ivtb);
                    message.sendToTarget();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }


    }

    //获取图片的名称
    private String getFileName(String url) {
        return Md5Utils.encode(url) + ".jpg";

    }

    //ImageView转换成Bitmap转换成Bitmap
    private class ImageViewToBitmap {
        ImageView iv;
        Bitmap bitmap;

        public ImageViewToBitmap(ImageView iv, Bitmap bitmap) {
            this.iv = iv;
            this.bitmap = bitmap;
        }
    }
}


3:加密

public class Md5Utils {
    public static String encode(String password){
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] result = digest.digest(password.getBytes());
            StringBuffer sb = new StringBuffer();
            for(byte b : result){
                int number = (int)(b & 0xff) ;
                String str = Integer.toHexString(number);
                if(str.length()==1){
                    sb.append("0");
                }
                sb.append(str);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            //can't reach
            return "";
        }
    }
}

4:MainActivity 方法

public class MainActivity extends AppCompatActivity {
    private String url = "http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fe871b36bad64034f79f019d4.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        ImageView iv = (ImageView) findViewById(R.id.iv);
        //实例化图片加载框架
        BitmapUtils bitmapUtils = new BitmapUtils(this);
        bitmapUtils.display(iv, url);


    }
}


### Spring 框架中的三级缓存机制 在Spring框架中,为了有效解决循环依赖问题并提供灵活可靠的Bean管理,采用了三级缓存结构。这不仅解决了复杂场景下的依赖关系难题,还确保了Bean初始化过程中的有序性和一致性。 #### 单例模式下Bean的创建流程与三级缓存的关系 当一个单例作用域内的Bean被实例化时,其创建过程中会经历三个阶段性的存储区域: 1. **earlySingletonObjects (一级缓存)** 这是一个`Object`类型的Map集合,在完成对象实例化之后立即将该对象放入此映射表中。此时的对象已经完成了构造函数调用,但尚未经过属性填充和其他后置处理器处理[^1]。 2. **singletonFactories (二级缓存)** 如果某个Bean正处于创建状态而其他地方又请求获取它,则先将其工厂方法封装成`ObjectFactory`形式暂存在这里。这样可以在不破坏当前构建链的情况下提前返回一个可访问的目标对象给外部使用者[^2]。 3. **singletonObjects (三级缓存)** 经过所有必要的初始化操作(包括设置属性值、应用AOP代理等),最终形成的完全可用版本会被放置于此处供后续查询使用。只有到达这个级别才算真正意义上的“已准备好”的单例Bean[^3]。 #### 循环依赖的具体应对策略 对于可能出现的循环引用情况——即两个或多个Bean之间相互持有对方作为成员变量的情形,Spring利用上述提到的不同层次的缓存来进行巧妙规避: - 当检测到有未完成初始化却已被索取的情况发生时,便会尝试从`singletonFactories`取出对应的半成品对象供给需求方; - 同时继续推进原定计划直至整个链条上的每一个参与者都达到可以正常工作的程度再统一移入至最顶层(`singletonObjects`)等待下次重访; 值得注意的是,并不是所有的循环依赖都能得到妥善处置。特别是那些涉及构造器参数传递型别的循环依赖就无法借助这套体系来化解,因为这类情形往往意味着至少有一个环节还没有开始就被迫终止了[^4]。 ```java // 示例代码展示了一个简单的循环依赖案例以及如何通过配置让Spring自动处理这种情况 @Configuration public class AppConfig { @Bean public BeanA beanA() { return new BeanA(beanB()); } @Bean public BeanB beanB() { return new BeanB(beanA()); } } class BeanA { private final BeanB b; public BeanA(BeanB b) { this.b = b; } // getters and setters... } class BeanB { private final BeanA a; public BeanB(BeanA a) {this.a = a;} // getters and setters... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值