Skip to content

Commit

Permalink
Ensure cache clear clears correct function (#6781)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Apr 23, 2024
1 parent 460c730 commit d5911ba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Releases

## Version 1.4.1
## Version 1.4.2

Date: 2024-04-19
Date: 2024-04-23

This micro-release fixes a number of smaller regressions and bugs including parsing of notebooks. Many thanks to our new contributor @bkreider, our returning contributors, @jrycw and @ea42gh, and our dedicated team of maintainers including @ahuang11, @MarcSkovMadsen and @philippjfr.

Expand All @@ -23,6 +23,9 @@ This micro-release fixes a number of smaller regressions and bugs including pars
- Fix styling of loading indicator in Fast design ([#6761](https://github.com/holoviz/panel/pull/6761))
- Ensure `VTK` nan, above and below colors are serialized ([#6763](https://github.com/holoviz/panel/pull/6763))
- Fix issues with `Perspective` theme and persist config when switching plugins ([#6764](https://github.com/holoviz/panel/pull/6764))
- Do not restore unmodified parameters in `config.set` triggering undesirable side-effects ([#6772 ([#6771](https://github.com/holoviz/panel/pull/6772))
- Make autoreload module cleanup more robust ([#6771](https://github.com/holoviz/panel/pull/6771))
- Ensure that cache `.clear()` clears the correct function ([#6771](https://github.com/holoviz/panel/pull/6781))

### Documentation

Expand Down
5 changes: 4 additions & 1 deletion doc/about/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ See [the HoloViz blog](https://blog.holoviz.org/#category=panel) for a visual su

## Version 1.4.2

Date: 2024-04-19
Date: 2024-04-23

This micro-release fixes a number of smaller regressions and bugs including parsing of notebooks. Many thanks to our new contributor @bkreider, our returning contributors, @jrycw and @ea42gh, and our dedicated team of maintainers including @ahuang11, @MarcSkovMadsen and @philippjfr.

Expand All @@ -25,6 +25,9 @@ This micro-release fixes a number of smaller regressions and bugs including pars
- Fix styling of loading indicator in Fast design ([#6761](https://github.com/holoviz/panel/pull/6761))
- Ensure `VTK` nan, above and below colors are serialized ([#6763](https://github.com/holoviz/panel/pull/6763))
- Fix issues with `Perspective` theme and persist config when switching plugins ([#6764](https://github.com/holoviz/panel/pull/6764))
- Do not restore unmodified parameters in `config.set` triggering undesirable side-effects ([#6772 ([#6771](https://github.com/holoviz/panel/pull/6772))
- Make autoreload module cleanup more robust ([#6771](https://github.com/holoviz/panel/pull/6771))
- Ensure that cache `.clear()` clears the correct function ([#6771](https://github.com/holoviz/panel/pull/6781))

### Documentation

Expand Down
14 changes: 6 additions & 8 deletions panel/io/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,11 @@ def cache(
cache_path=cache_path,
per_session=per_session,
)
func_hash = None # noqa
func_hashes = [None] # noqa

lock = threading.RLock()

def hash_func(*args, **kwargs):
global func_hash
# Handle param.depends method by adding parameters to arguments
func_name = func.__name__
is_method = (
Expand Down Expand Up @@ -391,6 +390,7 @@ def hash_func(*args, **kwargs):
func_hash += (id(state.curdoc),)
func_hash = hashlib.sha256(_generate_hash(func_hash)).hexdigest()

func_hashes[0] = func_hash
func_cache = state._memoize_cache.get(func_hash)

if func_cache is None:
Expand Down Expand Up @@ -439,13 +439,11 @@ def wrapped_func(*args, **kwargs):
func_cache[hash_value] = (ret, time, 0, time)
return ret

def clear():
global func_hash
def clear(func_hashes=func_hashes):
# clear called before anything is cached.
if 'func_hash' not in globals():
return
if func_hash is None:
if func_hashes[0] is None:
return
func_hash = func_hashes[0]
if to_disk:
from diskcache import Index
cache = Index(os.path.join(cache_path, func_hash))
Expand All @@ -457,7 +455,7 @@ def clear():
wrapped_func.clear = clear

if per_session and state.curdoc and state.curdoc.session_context:
def server_clear(session_context):
def server_clear(session_context, clear=clear):
clear()
state.curdoc.on_session_destroyed(server_clear)

Expand Down
24 changes: 24 additions & 0 deletions panel/tests/io/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,27 @@ def test_hash_on_simple_dataframes():
])
def test_is_equal(value, other, expected):
assert is_equal(value, other)==expected

def test_cache_clear_two_funcs():
# see https://github.com/holoviz/panel/issues/6777
@cache()
def get_data_1(a=[0]):
v = a[0]
a[0] = v+1
return v

@cache()
def get_data_2(b=[0]):
v = b[0]
b[0] = v+1
return v

assert get_data_1() == 0
assert get_data_2() == 0

get_data_1.clear()

assert get_data_1() == 1
assert get_data_2() == 0
assert get_data_1() == 1
assert get_data_2() == 0

0 comments on commit d5911ba

Please sign in to comment.