Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions beanprice/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import datetime
import functools
from os import path
import shelve
import os
import tempfile
import hashlib
import re
import sys
import logging
from concurrent import futures
from typing import Any, Dict, List, Optional, NamedTuple, Tuple
import diskcache

from dateutil import tz

Expand Down Expand Up @@ -557,6 +558,11 @@ def setup_cache(cache_filename: Optional[str], clear_cache: bool):
if not cache_filename:
return

# Handle switch to diskcache. Check for existing cache file and remove it,
# so that diskcache can create a directory with the same name.
if os.path.exists(cache_filename) and os.path.isfile(cache_filename):
os.remove(cache_filename)

logging.info('Using price cache at "%s" (with indefinite expiration)', cache_filename)

flag = "c"
Expand All @@ -565,14 +571,16 @@ def setup_cache(cache_filename: Optional[str], clear_cache: bool):
flag = "n"

global _CACHE
_CACHE = shelve.open(cache_filename, flag=flag) # type: ignore
_CACHE = diskcache.Cache(cache_filename, flag=flag)
_CACHE.expiration = DEFAULT_EXPIRATION # type: ignore


def reset_cache():
"""Reset the cache to its uninitialized state."""
global _CACHE
if _CACHE is not None:
if _CACHE.clear is not None:
_CACHE.clear()
_CACHE.close()
_CACHE = None

Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
'python-dateutil >= 2.6.0',
'requests >= 2.0',
'curl_cffi>=0.6.5',
"diskcache>=5.6.3",
]

[project.scripts]
Expand All @@ -42,6 +43,13 @@ bean-price = 'beanprice.price:main'
homepage = 'https://github.com/beancount/beanprice'
issues = 'https://github.com/beancount/beanprice/issues'

[dependency-groups]
dev = [
"pytest>=8.3.5",
"pylint==3.3.3",
"mypy==1.14.1"
]

[tool.setuptools.packages]
find = {}

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
beancount>=3.0.0
python-dateutil>=2.6.0
requests>=2.0
curl_cffi>=0.6.5
curl_cffi>=0.6.5
diskcache>=5.6.3