Provides pickled object caching. @cvar protocol: The pickling protocol. @type protocol: int
| 357 | |
| 358 | |
| 359 | class ObjectCache(FileCache): |
| 360 | """ |
| 361 | Provides pickled object caching. |
| 362 | @cvar protocol: The pickling protocol. |
| 363 | @type protocol: int |
| 364 | """ |
| 365 | protocol = 2 |
| 366 | |
| 367 | def fnsuffix(self): |
| 368 | return 'px' |
| 369 | |
| 370 | def get(self, id): |
| 371 | try: |
| 372 | with FileCache.getf(self, id) as fp: |
| 373 | if fp is None: |
| 374 | return None |
| 375 | else: |
| 376 | return pickle.load(fp) |
| 377 | except: |
| 378 | FileCache.purge(self, id) |
| 379 | |
| 380 | def put(self, id, object): |
| 381 | bfr = pickle.dumps(object, self.protocol) |
| 382 | FileCache.put(self, id, bfr) |
| 383 | return object |