summaryrefslogtreecommitdiff
path: root/pysnmp/proto/cache.py
blob: 9eae19c9019305a650cbb62145cb47d8bb449410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
from pysnmp.proto import error


class Cache(object):
    def __init__(self):
        self.__cacheRepository = {}

    def add(self, index, **kwargs):
        self.__cacheRepository[index] = kwargs
        return index

    def pop(self, index):
        if index in self.__cacheRepository:
            cachedParams = self.__cacheRepository[index]
        else:
            return
        del self.__cacheRepository[index]
        return cachedParams

    def update(self, index, **kwargs):
        if index not in self.__cacheRepository:
            raise error.ProtocolError(
                'Cache miss on update for %s' % kwargs
            )
        self.__cacheRepository[index].update(kwargs)

    def expire(self, cbFun, cbCtx):
        for index, cachedParams in list(self.__cacheRepository.items()):
            if cbFun:
                if cbFun(index, cachedParams, cbCtx):
                    if index in self.__cacheRepository:
                        del self.__cacheRepository[index]