Skip to content

Commit

Permalink
fix for cache (#3538)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan authored Oct 17, 2023
1 parent 9c1e89f commit fdc96f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
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()

0 comments on commit fdc96f8

Please sign in to comment.