1
1
from . import _core
2
2
from ._core import BaseCacheImpl
3
3
from datetime import timedelta , datetime
4
+ import copy as _std_copy
4
5
import typing
5
6
6
7
11
12
12
13
def _items_to_str (items , length ):
13
14
if length <= 50 :
14
- return "{" + ", " .join (f"{ k } : { v } " for k , v in items ) + "}"
15
+ return "{" + ", " .join (f"{ k !r } : { v !r } " for k , v in items ) + "}"
15
16
16
17
c = 0
17
18
left = []
@@ -20,7 +21,7 @@ def _items_to_str(items, length):
20
21
k , v = next (items )
21
22
22
23
if c <= 50 :
23
- left .append (f"{ k } : { v } " )
24
+ left .append (f"{ k !r } : { v !r } " )
24
25
25
26
else :
26
27
break
@@ -251,6 +252,22 @@ def values(self) -> IteratorView[VT]:
251
252
"""
252
253
return IteratorView (self ._raw .items (), lambda x : x [1 ])
253
254
255
+ def copy (self ) -> "Cache[KT, VT]" :
256
+ """Returns a shallow copy of the cache"""
257
+ return self .__copy__ ()
258
+
259
+ def __copy__ (self ) -> "Cache[KT, VT]" :
260
+ cls = type (self )
261
+ copied = cls .__new__ (cls )
262
+ copied ._raw = _std_copy .copy (self ._raw )
263
+ return copied
264
+
265
+ def __deepcopy__ (self , memo ) -> "Cache[KT, VT]" :
266
+ cls = type (self )
267
+ copied = cls .__new__ (cls )
268
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
269
+ return copied
270
+
254
271
def __iter__ (self ) -> IteratorView [KT ]:
255
272
return self .keys ()
256
273
@@ -501,6 +518,22 @@ def last(self) -> typing.Optional[KT]:
501
518
"""
502
519
return self ._raw .get_index (len (self ._raw ) - 1 )
503
520
521
+ def copy (self ) -> "FIFOCache[KT, VT]" :
522
+ """Returns a shallow copy of the cache"""
523
+ return self .__copy__ ()
524
+
525
+ def __copy__ (self ) -> "FIFOCache[KT, VT]" :
526
+ cls = type (self )
527
+ copied = cls .__new__ (cls )
528
+ copied ._raw = _std_copy .copy (self ._raw )
529
+ return copied
530
+
531
+ def __deepcopy__ (self , memo ) -> "FIFOCache[KT, VT]" :
532
+ cls = type (self )
533
+ copied = cls .__new__ (cls )
534
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
535
+ return copied
536
+
504
537
def __iter__ (self ) -> IteratorView [KT ]:
505
538
return self .keys ()
506
539
@@ -739,6 +772,22 @@ def values(self) -> IteratorView[VT]:
739
772
"""
740
773
return IteratorView (self ._raw .items (), lambda x : x [1 ])
741
774
775
+ def copy (self ) -> "RRCache[KT, VT]" :
776
+ """Returns a shallow copy of the cache"""
777
+ return self .__copy__ ()
778
+
779
+ def __copy__ (self ) -> "RRCache[KT, VT]" :
780
+ cls = type (self )
781
+ copied = cls .__new__ (cls )
782
+ copied ._raw = _std_copy .copy (self ._raw )
783
+ return copied
784
+
785
+ def __deepcopy__ (self , memo ) -> "RRCache[KT, VT]" :
786
+ cls = type (self )
787
+ copied = cls .__new__ (cls )
788
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
789
+ return copied
790
+
742
791
def __iter__ (self ) -> IteratorView [KT ]:
743
792
return self .keys ()
744
793
@@ -995,6 +1044,22 @@ def most_recently_used(self) -> typing.Optional[KT]:
995
1044
"""
996
1045
return self ._raw .most_recently_used ()
997
1046
1047
+ def copy (self ) -> "LRUCache[KT, VT]" :
1048
+ """Returns a shallow copy of the cache"""
1049
+ return self .__copy__ ()
1050
+
1051
+ def __copy__ (self ) -> "LRUCache[KT, VT]" :
1052
+ cls = type (self )
1053
+ copied = cls .__new__ (cls )
1054
+ copied ._raw = _std_copy .copy (self ._raw )
1055
+ return copied
1056
+
1057
+ def __deepcopy__ (self , memo ) -> "LRUCache[KT, VT]" :
1058
+ cls = type (self )
1059
+ copied = cls .__new__ (cls )
1060
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
1061
+ return copied
1062
+
998
1063
def __iter__ (self ) -> IteratorView [KT ]:
999
1064
return self .keys ()
1000
1065
@@ -1264,6 +1329,22 @@ def least_frequently_used(self, n: int = 0) -> typing.Optional[KT]:
1264
1329
1265
1330
return self ._raw .least_frequently_used (n )
1266
1331
1332
+ def copy (self ) -> "LFUCache[KT, VT]" :
1333
+ """Returns a shallow copy of the cache"""
1334
+ return self .__copy__ ()
1335
+
1336
+ def __copy__ (self ) -> "LFUCache[KT, VT]" :
1337
+ cls = type (self )
1338
+ copied = cls .__new__ (cls )
1339
+ copied ._raw = _std_copy .copy (self ._raw )
1340
+ return copied
1341
+
1342
+ def __deepcopy__ (self , memo ) -> "LFUCache[KT, VT]" :
1343
+ cls = type (self )
1344
+ copied = cls .__new__ (cls )
1345
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
1346
+ return copied
1347
+
1267
1348
def __iter__ (self ) -> IteratorView [KT ]:
1268
1349
return self .keys ()
1269
1350
@@ -1605,6 +1686,22 @@ def expire(self) -> None: # pragma: no cover
1605
1686
"""
1606
1687
self ._raw .expire ()
1607
1688
1689
+ def copy (self ) -> "TTLCache[KT, VT]" :
1690
+ """Returns a shallow copy of the cache"""
1691
+ return self .__copy__ ()
1692
+
1693
+ def __copy__ (self ) -> "TTLCache[KT, VT]" :
1694
+ cls = type (self )
1695
+ copied = cls .__new__ (cls )
1696
+ copied ._raw = _std_copy .copy (self ._raw )
1697
+ return copied
1698
+
1699
+ def __deepcopy__ (self , memo ) -> "TTLCache[KT, VT]" :
1700
+ cls = type (self )
1701
+ copied = cls .__new__ (cls )
1702
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
1703
+ return copied
1704
+
1608
1705
def __iter__ (self ) -> IteratorView [KT ]:
1609
1706
return self .keys ()
1610
1707
@@ -1982,6 +2079,22 @@ def expire(self) -> None: # pragma: no cover
1982
2079
"""
1983
2080
self ._raw .expire ()
1984
2081
2082
+ def copy (self ) -> "VTTLCache[KT, VT]" :
2083
+ """Returns a shallow copy of the cache"""
2084
+ return self .__copy__ ()
2085
+
2086
+ def __copy__ (self ) -> "VTTLCache[KT, VT]" :
2087
+ cls = type (self )
2088
+ copied = cls .__new__ (cls )
2089
+ copied ._raw = _std_copy .copy (self ._raw )
2090
+ return copied
2091
+
2092
+ def __deepcopy__ (self , memo ) -> "VTTLCache[KT, VT]" :
2093
+ cls = type (self )
2094
+ copied = cls .__new__ (cls )
2095
+ copied ._raw = _std_copy .deepcopy (self ._raw , memo )
2096
+ return copied
2097
+
1985
2098
def __iter__ (self ) -> IteratorView [KT ]:
1986
2099
return self .keys ()
1987
2100
0 commit comments