Skip to content

Commit deacf88

Browse files
authored
Merge pull request #24 from KnownBlackHat/feat/caching
2 parents 04f1f24 + 01d27a5 commit deacf88

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- repo: https://github.com/psf/black
9+
rev: 23.3.0
10+
hooks:
11+
- id: black
12+
- repo: https://github.com/PyCQA/isort
13+
rev: 5.12.0
14+
hooks:
15+
- id: isort
16+
exclude: '^(env)'

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ Using this for the first time? Here are some links to help you get started.
1111
- [Report bugs](https://github.com/shahriyardx/easy-pil/issues/)
1212

1313
## Get help
14-
- Ask us in our [Discord server](https://discord.gg/4rd4JQWmsY)
14+
- Ask us in our [Discord server](https://discord.gg/fVzt5THTNb)
1515
- Watch [Youtube Tutorials](https://www.youtube.com/playlist?list=PLb_oBhGqAlbT4yVqV0TSXggA8b0lZhGhn)

easy_pil/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import aiohttp
77
import requests
8-
from async_lru import alru_cache
8+
from aiocache import cached
99
from PIL import Image
1010

1111

@@ -22,7 +22,7 @@ async def run_in_executor(func, **kwargs):
2222
return data
2323

2424

25-
@lru_cache()
25+
@lru_cache(maxsize=32)
2626
def load_image(link: str) -> Image.Image:
2727
"""Load image from link
2828
@@ -42,8 +42,8 @@ def load_image(link: str) -> Image.Image:
4242
return image
4343

4444

45-
@alru_cache()
46-
async def load_image_async(link: str) -> Image.Image:
45+
@cached(ttl=60 * 60 * 24)
46+
async def load_image_async(link: str, session=None) -> Image.Image:
4747
"""Load image from link (async)
4848
4949
Parameters
@@ -56,9 +56,13 @@ async def load_image_async(link: str) -> Image.Image:
5656
PIL.Image.Image
5757
Image link
5858
"""
59-
async with aiohttp.ClientSession() as session:
60-
async with session.get(link) as response:
59+
if isinstance(session, aiohttp.ClientSession):
60+
async with session.get(link) as response: # type: ignore
6161
data = await response.read()
62+
else:
63+
async with aiohttp.ClientSession() as session:
64+
async with session.get(link) as response:
65+
data = await response.read()
6266

6367
_bytes = BytesIO(data)
6468
image = Image.open(_bytes).convert("RGBA")

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Pillow>=9.5.0
22
requests>=2.30.0
33
typing-extensions>=4.5.0
44
aiohttp>=3.6.0
5-
async_lru>=2.0.2
5+
aiocache<=0.12.1

0 commit comments

Comments
 (0)