Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for cache #3538

Merged
merged 2 commits into from
Oct 17, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## Unreleased

### Fixed

- Fixed issue with LRUCache.discard https://github.com/Textualize/textual/issues/3537

### Changed

- Buttons will now display multiple lines, and have auto height https://github.com/Textualize/textual/pull/3539


## [0.40.0] - 2023-10-11

- Added `loading` reactive property to widgets https://github.com/Textualize/textual/pull/3509
Expand Down
12 changes: 10 additions & 2 deletions src/textual/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,22 @@ def discard(self, key: CacheKey) -> None:
Args:
key: Cache key.
"""
link = self._cache.get(key)
if link is None:
if key not in self._cache:
return
link = self._cache[key]

# Remove link from list
link[0][1] = link[1] # type: ignore[index]
link[1][0] = link[0] # type: ignore[index]
# Remove link from cache

if self._head[2] == key:
self._head = self._head[1] # type: ignore[assignment]
if self._head[2] == key: # type: ignore[index]
self._head = []

del self._cache[key]
self._full = False


class FIFOCache(Generic[CacheKey, CacheValue]):
Expand Down
1 change: 1 addition & 0 deletions src/textual/widgets/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def _render_line_strip(self, y: int, rich_style: Style) -> Strip:
line = Strip(line_text.render(self.app.console), cell_len(_line))
else:
line = Strip([Segment(_line, rich_style)], cell_len(_line))

self._render_line_cache[y] = line
return line

Expand Down
26 changes: 26 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,29 @@ def test_discard():
cache.discard("key4") # key that does not exist

assert len(cache) == 2 # size should not change


def test_discard_regression():
"""Regression test for https://github.com/Textualize/textual/issues/3537"""

cache = LRUCache(maxsize=3)
cache[1] = "foo"
cache[2] = "bar"
cache[3] = "baz"
cache[4] = "egg"

assert cache.keys() == {2, 3, 4}

cache.discard(2)
assert cache.keys() == {3, 4}

cache[5] = "bob"
assert cache.keys() == {3, 4, 5}

cache.discard(5)
assert cache.keys() == {3, 4}

cache.discard(4)
cache.discard(3)

assert cache.keys() == set()
Loading