当前位置:网站首页>Glide caching mechanism
Glide caching mechanism
2022-06-27 10:38:00 【Orange 19911016】
1 Glide Picture loading process

- Package parameters : From the specified source to the output , There may be many processes in the middle , So the first thing is to encapsulate the parameters , These parameters will run through the whole picture loading process ;
- Parsing path : There are many sources of pictures , The format is different , Need to standardize ;
- Read cache : In order to reduce the calculation , Usually cache , Priority read from cache ;
- Find files / Download the file : If it's a local file , Decode it directly , If it's a network picture , Need to download ;
- decode :
- Transformation : Decode out
Bitmapafter , You may need to do some transformation processing ( Round corners , Filters, etc ); - cache : To get the final
BitmapStop vomiting , It can be cached , For the next time ; - Show : Show results , You may need to do some animation ;
2 Glide Introduction to caching mechanism
2.1 Cached image resources
- Original picture (
Source) : That is, the initial size and resolution of the image source ; - The converted image (
Result) : A picture that has been resized and compressed ;
When using Glide When loading images ,Glide It will be based on View View to compress and convert pictures , Instead of displaying the original image .( This is also Glide Loading speed is higher than Picasso Why )
2.2 Cache mechanism design
Glide The cache function of is designed as L2 cache : Memory cache and hard disk cache .( Loading from the network does not belong to the cache )
- Memory cache : Prevent repeated reading of pictures into memory , Waste of memory resources , Only cache the converted pictures , Not the original picture ;
- Disk caching : Prevent repeated downloading and reading of data from the network or other places , It can cache the original image and the converted image , Set by the user ;
stay Glide in , The read order of the cache is : Memory cache –> Disk caching –> The Internet , Memory cache and disk cache do not affect each other , Independent configuration , Memory caching is enabled by default .
Glide The caching mechanism of makes Glide It has a very good picture caching effect , Thus, it has high picture loading efficiency .
Here are Glide Related code :
// Memory cache is enabled by default , The user does not need to make any settings
Glide.with(this).load(url).into(imageView);
// It can be done by API Disable memory caching
Glide.with(this).load(url).skipMemoryCache(true) // Disable memory caching
.into(imageView);
Glide.with(this).load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE) // Don't cache any pictures , That is, disable disk caching
.into(imageView);
// DiskCacheStrategy.NONE: Don't cache any pictures , That is, disable disk caching
// DiskCacheStrategy.ALL : Cache the original picture & The converted image
// DiskCacheStrategy.SOURCE: Cache only the original picture ( The original full resolution image , That is, the converted image is not cached )
// DiskCacheStrategy.RESULT:( Default ) Only cache the converted pictures ( The final image : After reducing the resolution / Or after conversion , Do not cache the original picture
2.3 Cache type
- Active cache (
ActiveResource): Store pictures in use ; LruMemory cache (LruResourceCache): After the image is parsed and recently loaded, it will be put into memory ;- Disk caching - The resource type (
DiskCache - Resource): The decoded image is written to the disk file ; - Disk caching - Raw data (
DiskCache - Data): After the network request succeeds or the local acquisition succeeds , Cache raw data on disk ;
Lru(Least Recently Used): Recently at least use , Its core idea is , When the cache is full , Priority will be given to the least recently used cache objects .

2.3.1 Memory cache / Runtime caching
Memory cache / The runtime cache is divided into two parts : Active cache (ActiveResource) and Lru Memory cache (LruResourceCache).
LinkedHashMap Inherited from HashMap, On this basis, the structure of two-way linked list is added , Every time you access data , Will update the linked list pointer of the accessed data . For example, deleting from a linked list is not really deleting data , Just moved the pointer of the linked list .

Lru Memory cache : Use LinkedHashMap To cache resources ( Strong citation ), And set a cache size . If a resource is accessed , First, the node will be deleted from the linked list , Then add it to the chain header , This ensures that the nodes in the head of the linked list are recently accessed . When the number of caches reaches the maximum , Will end the list ( Recently at least use ) Data removal .
But there is a risk , It is easy to recycle the resources in use .
Glide This design : From memory cache (LruResourceCache) When you get the resources in, you will actively add them to the active cache (ActiveResource) in , And clean up Lru Memory cache (LruResourceCache) The resource , The advantage of this is to protect the resources being used from being Lru Algorithm recycle .
ActiveResources It's a weakly quoted HashMap, Used to cache images in use , Saving this image will not be Lru Algorithm recycle . Pictures will be added to again when they are used up Lru In memory cache .
ActiveResources and LruResourceCache Is memory cache , Belongs to the runtime cache and is mutually exclusive ( The same picture will not be cached in ActiveResources and LruResourceCache in ), After the application is killed, the memory cache will not exist .
2.3.2 Disk caching
Disk caching strategy :
DiskCacheStrategy.NONE: Indicates that nothing is cached ;DiskCacheStrategy.RESOURCE: Write data to disk cache after resource decoding , That is, the image resources after scaling and other conversion ;DiskCacheStrategy.DATA: Write the original data to the disk cache before decoding the resource ;DiskCacheStrategy.ALL: UseDATAandRESOURCECache data ;DiskCacheStrategy.AUTOMATIC: It tries to use the best strategy for local and remote images . When loading remote data ,AUTOMATICThe policy only stores the original data that has not been modified by the loading process , Because downloading remote data is much more expensive than adjusting the data that already exists on the disk . For local data ,AUTOMATICThe policy stores only the transformed thumbnails , Because even if you need to generate another size or type of picture again , It's also easy to get back the raw data . This caching strategy is used by default ;
In the use of Glide To load an image ,Glide The original image will not be displayed by default , Instead, it compresses and transforms the image . We can cache the converted images , You can also cache the original image before conversion .
use LRU There are two kinds of caching algorithms :LruCache and DisLruCache, It is used to implement internal coarse cache and hard disk cache respectively .
3 Big picture loading
Huge for a single picture , And compression is not allowed . For example, display : Map of the world 、 Riverside Scene at Qingming Festival 、 Microblog long picture, etc .
First, do not compress , Load according to the size of the original drawing , Then the screen must not be big enough , And considering the memory , It is impossible to load the whole image into memory at one time , Therefore, the optimization idea in this case is generally local loading , adopt BitmapRegionDecoder To achieve .BitmapRegionDecoder A rectangular region can be decoded from the image , Show only part of the image . In this case, usually Glide Only responsible for downloading pictures , The loading of the picture is done by the custom ImageView To achieve .
After the picture is partitioned , Each block is loaded independently , And load bitmap It's a time-consuming operation , Will load bitmap Asynchronous implementation . After partitioning, there is also a logic to trigger loading and recycling , The principle is to trigger loading within the range it is displayed , Recycle after leaving the scope .
region [ˈriːdʒən] region , Area , world decoder [diːˈkoʊdər] decoder , Decoder ; Decoder
Reference resources
Glide Cache summary ( One )
Android Source code analysis : Take you hand in hand to analyze Glide Cache function of
Glide Caching mechanism of source code Glide Caching mechanism of source code
Android Glide Cache mechanism and source code
Talking about Glide Principle
Talk about Glide Those things in the interview
【Android Memory optimization 】Bitmap Long map loading ( BitmapRegionDecoder brief introduction | BitmapRegionDecoder Usage flow | Area decoding loading example )
Learning notes -android Large picture loading details
How to solve android A lot of picture frame animation Caton and OOM problem ?
边栏推荐
- 微软云 (Microsoft Cloud) 技术概述
- [tcapulusdb knowledge base] tcapulusdb Model Management Introduction
- 有关WIN10的内存压缩
- 用户认证技术
- [tcapulusdb knowledge base] tcapulusdb cluster management introduction
- torch. utils. data. Randomsampler and torch utils. data. Differences between sequentialsampler
- 【TcaplusDB知识库】TcaplusDB机型管理介绍
- 2021 CSP J2 entry group csp-s2 improvement group round 2 video and question solution
- Leetcode 729. 我的日程安排表 I(提供一种思路)
- Arduino PROGMEM静态存储区的使用介绍
猜你喜欢

CPU设计(单周期和流水线)

“全班29人24人成功读研”冲上热搜!剩下的5个人去哪了?
![leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]](/img/70/3954b0871cc31d24ae016eb99d871e.png)
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]

Explain the imaging principle of various optical instruments in detail
![[methodot topic] what kind of low code platform is more suitable for developers?](/img/1e/934b7e013db400332bc77c081f9f29.png)
[methodot topic] what kind of low code platform is more suitable for developers?

通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑

Go zero micro Service Practice Series (VII. How to optimize such a high demand)

ci/cd自动化测试_CI / CD管道加快测试自动化的16种最佳实践

微软云 (Microsoft Cloud) 技术概述

2-4 installation of Nessus under Kali
随机推荐
C language learning day_ 04
Queue, two-way queue, and its application
Memory compression for win10
3D移动 translate3d
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
居家办公竟比去公司上班还累? | 社区征文
Based on swift admin's rapid background development framework, I made a rookie tutorial [professional version]
lvi-sam 总结
感应电机直接转矩控制系统的设计与仿真(运动控制matlab/simulink)
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
C语言学习-Day_04
Leetcode to do questions
【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
JS file upload and download
[STM32] Hal library stm32cubemx tutorial 12 - IIC (read AT24C02)
audiotrack与audioflinger
Array object in JS
【TcaplusDB知识库】TcaplusDB机器初始化和上架介绍
Eureka核心源码解析
. Net