summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Hutchinson <jlhutch@gmail.com>2022-02-26 11:56:02 -0600
committerJay Hutchinson <jlhutch@gmail.com>2022-02-26 18:15:02 -0600
commit605856ff6f5993e851032d7d64a945396a1ffb0c (patch)
treea7911c313b4c117821667d74140375ccda28dc5d
parent6411a3cf50ea96f513784c49b7154e49f54df3e8 (diff)
downloadpylru-605856ff6f5993e851032d7d64a945396a1ffb0c.tar.gz
Added __getstate__() and __setstate__().
-rw-r--r--pylru.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/pylru.py b/pylru.py
index c751e37..67f3d7d 100644
--- a/pylru.py
+++ b/pylru.py
@@ -317,6 +317,38 @@ class lrucache(object):
yield node
node = node.next
+ def __getstate__(self):
+ d = self.__dict__.copy()
+ del d['table']
+ del d['head']
+
+ elements = [(node.key, node.value) for node in self.dli()]
+ return (d, elements)
+
+ def __setstate__(self, state):
+ d = state[0]
+ elements = state[1]
+
+ self.__dict__.update(d)
+
+
+ size = self.listSize
+
+ # Create an empty hash table.
+ self.table = {}
+
+ self.head = _dlnode()
+ self.head.next = self.head
+ self.head.prev = self.head
+
+ self.listSize = 1
+
+ # Now adjust the list to the desired size.
+ self.size(size)
+
+
+ for key, value in reversed(elements):
+ self[key] = value
class WriteThroughCacheManager(object):