Skip to content

Commit 0b58619

Browse files
committed
fix: utils
1 parent 286dd8d commit 0b58619

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/typed_diskcache/utils/deque.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from typed_diskcache import Cache
1313
from typed_diskcache import exception as te
14-
from typed_diskcache.core.context import context
14+
from typed_diskcache.core.context import context, enter_session
1515
from typed_diskcache.core.types import EvictionPolicy, SettingsKwargs
1616
from typed_diskcache.database.connect import transact
1717
from typed_diskcache.log import get_logger
@@ -120,10 +120,11 @@ def main() -> None:
120120
```
121121
"""
122122
self._maxlen = value
123-
with self.cache.conn.connect() as sa_conn:
124-
with transact(sa_conn):
125-
while len(self.cache) > self._maxlen:
126-
self.popleft()
123+
with self.cache.conn.session() as session:
124+
with transact(session):
125+
with enter_session(session) as context:
126+
while len(self.cache) > self._maxlen:
127+
context.run(self.popleft)
127128

128129
@context("Deque.append")
129130
@override
@@ -147,11 +148,12 @@ def main() -> None:
147148
# ['a', 'b', 'c']
148149
```
149150
"""
150-
with self.cache.conn.connect() as sa_conn:
151-
with transact(sa_conn):
152-
self.cache.push(value, side="back", retry=True)
153-
if len(self.cache) > self._maxlen:
154-
self.popleft()
151+
with self.cache.conn.session() as session:
152+
with transact(session):
153+
with enter_session(session) as context:
154+
context.run(self.cache.push, value, side="back", retry=True)
155+
if len(self.cache) > self._maxlen:
156+
context.run(self.popleft)
155157

156158
@context("Deque.appendleft")
157159
def appendleft(self, value: _T) -> None:
@@ -174,11 +176,12 @@ def main() -> None:
174176
# ['c', 'b', 'a']
175177
```
176178
"""
177-
with self.cache.conn.connect() as sa_conn:
178-
with transact(sa_conn):
179-
self.cache.push(value, side="front", retry=True)
180-
if len(self.cache) > self._maxlen:
181-
self.pop()
179+
with self.cache.conn.session() as session:
180+
with transact(session):
181+
with enter_session(session) as context:
182+
context.run(self.cache.push, value, side="front", retry=True)
183+
if len(self.cache) > self._maxlen:
184+
context.run(self.pop)
182185

183186
def copy(self) -> Self:
184187
"""Copy deque with same directory and max length."""

0 commit comments

Comments
 (0)