Skip to content

Commit

Permalink
Make cache public (Textualize#3976)
Browse files Browse the repository at this point in the history
* pulic cache

* changelog

* Apply suggestions from code review

Co-authored-by: Rodrigo Girão Serrão <[email protected]>

---------

Co-authored-by: Rodrigo Girão Serrão <[email protected]>
  • Loading branch information
willmcgugan and rodrigogiraoserrao authored Jan 8, 2024
1 parent 054a132 commit b61b878
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- Breaking change: `DOMNode.has_pseudo_class` now accepts a single name only https://github.com/Textualize/textual/pull/3970
- Made `textual.cache` (formerly `textual._cache`) public https://github.com/Textualize/textual/pull/3976

### Added

Expand Down
1 change: 1 addition & 0 deletions docs/api/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: textual.cache
1 change: 1 addition & 0 deletions mkdocs-nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ nav:
- "api/await_complete.md"
- "api/await_remove.md"
- "api/binding.md"
- "api/cache.md"
- "api/color.md"
- "api/command.md"
- "api/containers.md"
Expand Down
26 changes: 17 additions & 9 deletions src/textual/_cache.py → src/textual/cache.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
"""
A LRU (Least Recently Used) Cache container.
Containers to implement caching.
Use when you want to cache slow operations and new keys are a good predictor
of subsequent keys.
These are used in Textual to avoid recalculating expensive operations, such as rendering.
Note that stdlib's @lru_cache is implemented in C and faster! It's best to use
@lru_cache where you are caching things that are fairly quick and called many times.
Use LRUCache where you want increased flexibility and you are caching slow operations
where the overhead of the cache is a small fraction of the total processing time.
"""

from __future__ import annotations
Expand All @@ -34,6 +29,11 @@ class LRUCache(Generic[CacheKey, CacheValue]):
Each entry is stored as [PREV, NEXT, KEY, VALUE] where PREV is a reference
to the previous entry, and NEXT is a reference to the next value.
Note that stdlib's @lru_cache is implemented in C and faster! It's best to use
@lru_cache where you are caching things that are fairly quick and called many times.
Use LRUCache where you want increased flexibility and you are caching slow operations
where the overhead of the cache is a small fraction of the total processing time.
"""

__slots__ = [
Expand All @@ -46,6 +46,11 @@ class LRUCache(Generic[CacheKey, CacheValue]):
]

def __init__(self, maxsize: int) -> None:
"""Initialize a LRUCache.
Args:
maxsize: Maximum size of the cache, before old items are discarded.
"""
self._maxsize = maxsize
self._cache: Dict[CacheKey, list[object]] = {}
self._full = False
Expand Down Expand Up @@ -208,8 +213,6 @@ class FIFOCache(Generic[CacheKey, CacheValue]):
It is most suitable for a cache with a relatively low maximum size that is not expected to
do many lookups.
Args:
maxsize: Maximum size of the cache.
"""

__slots__ = [
Expand All @@ -220,6 +223,11 @@ class FIFOCache(Generic[CacheKey, CacheValue]):
]

def __init__(self, maxsize: int) -> None:
"""Initialize a FIFOCache.
Args:
maxsize: Maximum size of cache before discarding items.
"""
self._maxsize = maxsize
self._cache: dict[CacheKey, CacheValue] = {}
self.hits = 0
Expand Down
2 changes: 1 addition & 1 deletion src/textual/css/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from rich.panel import Panel
from rich.text import Text

from .._cache import LRUCache
from ..cache import LRUCache
from ..dom import DOMNode
from ..widget import Widget
from .errors import StylesheetError
Expand Down
2 changes: 1 addition & 1 deletion src/textual/fuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from rich.style import Style
from rich.text import Text

from ._cache import LRUCache
from .cache import LRUCache


@rich.repr.auto
Expand Down
2 changes: 1 addition & 1 deletion src/textual/strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from rich.segment import Segment
from rich.style import Style, StyleType

from ._cache import FIFOCache
from ._segment_tools import index_to_cell_position
from .cache import FIFOCache
from .color import Color
from .constants import DEBUG
from .filter import LineFilter
Expand Down
2 changes: 1 addition & 1 deletion src/textual/suggester.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dataclasses import dataclass
from typing import Iterable

from ._cache import LRUCache
from .cache import LRUCache
from .dom import DOMNode
from .message import Message

Expand Down
2 changes: 1 addition & 1 deletion src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from . import constants, errors, events, messages
from ._animator import DEFAULT_EASING, Animatable, BoundAnimator, EasingFunction
from ._arrange import DockArrangeResult, arrange
from ._cache import FIFOCache
from ._compose import compose
from ._context import NoActiveAppError, active_app
from ._easing import DEFAULT_SCROLL_EASING
Expand All @@ -52,6 +51,7 @@
from .actions import SkipAction
from .await_remove import AwaitRemove
from .box_model import BoxModel
from .cache import FIFOCache
from .css.query import NoMatches, WrongType
from .css.scalar import ScalarOffset
from .dom import DOMNode, NoScreen
Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from typing_extensions import Literal, Self, TypeAlias

from .. import events
from .._cache import LRUCache
from .._segment_tools import line_crop
from .._two_way_dict import TwoWayDict
from .._types import SegmentLines
from ..binding import Binding, BindingType
from ..cache import LRUCache
from ..color import Color
from ..coordinate import Coordinate
from ..geometry import Region, Size, Spacing, clamp
Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from rich.text import Text

from .. import work
from .._cache import LRUCache
from .._line_split import line_split
from ..cache import LRUCache
from ..geometry import Size
from ..reactive import var
from ..scroll_view import ScrollView
Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_rich_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rich.segment import Segment
from rich.text import Text

from .._cache import LRUCache
from ..cache import LRUCache
from ..geometry import Region, Size
from ..reactive import var
from ..scroll_view import ScrollView
Expand Down
2 changes: 1 addition & 1 deletion src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from rich.text import Text, TextType

from .. import events
from .._cache import LRUCache
from .._immutable_sequence_view import ImmutableSequenceView
from .._loop import loop_last
from .._segment_tools import line_pad
from ..binding import Binding, BindingType
from ..cache import LRUCache
from ..geometry import Region, Size, clamp
from ..message import Message
from ..reactive import reactive, var
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from textual._cache import FIFOCache, LRUCache
from textual.cache import FIFOCache, LRUCache


def test_lru_cache():
Expand Down

0 comments on commit b61b878

Please sign in to comment.