# gpui: Add ImageCache \(\#27774\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/abf2b9d7d315566c53130ed9f6d8c1476e70dc80)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: abf2b9d7d315566c53130ed9f6d8c1476e70dc80

Requested commit: abf2b9d7d315566c53130ed9f6d8c1476e70dc80

Commit: abf2b9d7d315566c53130ed9f6d8c1476e70dc80

Tree: d087ffadb7734742769f35828f7e49239965e3e4

Author: Sunli

Committer: GitHub

## Message

```
gpui: Add ImageCache (#27774)

Closes #27414

`ImageCache` is independent of the original image loader and can
actively release its cached images to solve the problem of images loaded
from the network or files not being released.

It has two constructors:

- `ImageCache::new`: Manually manage the cache.
- `ImageCache::max_items`: Remove the least recently used items when the
cache reaches the specified number.

When creating an `img` element, you can specify the cache object with
`Img::cache`, and the image cache will be managed by `ImageCache`.

In the example `crates\gpui\examples\image-gallery.rs`, the
`ImageCache::clear` method is actively called when switching a set of
images, and the memory will no longer continuously increase.


Release Notes:

- N/A

---------

Co-authored-by: Ben Kunkle <ben@zed.dev>
```

## Parents

- [a50fbc9b5c8aab7324c40d8bd63809e715ae5fde](https://git.cafe/gitcafe/zed/commit/a50fbc9b5c8aab7324c40d8bd63809e715ae5fde?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/abf2b9d7d315566c53130ed9f6d8c1476e70dc80?format=markdown)
