DictCache

DictCache.

class DIRAC.Core.Utilities.DictCache.DictCache(deleteFunction=False, threadLocal=False)

Bases: object

DictCache is a generic cache implementation. The user can decide whether this cache should be shared among the threads or not, but it is always thread safe Note that when shared, the access to the cache is protected by a lock, but not necessarily the object you are retrieving from it.

__init__(deleteFunction=False, threadLocal=False)

Initialize the dict cache.

Parameters:
  • deleteFunction – if not False, invoked when deleting a cached object

  • threadLocal – if False, the cache will be shared among all the threads, otherwise, each thread gets its own cache.

add(cKey, validSeconds, value=None)

Add a record to the cache

Parameters:
  • cKey – identification key of the record

  • validSeconds (int) – valid seconds of this record

  • value – value of the record

delete(cKey)

Delete a key from the cache

Parameters:

cKey – identification key of the record

exists(cKey, validSeconds=0)

Returns True/False if the key exists for the given number of seconds

Parameters:
  • cKey – identification key of the record

  • validSeconds (int) – The amount of seconds the key has to be valid for

Returns:

bool

get(cKey, validSeconds=0)

Get a record from the cache

Parameters:
  • cKey – identification key of the record

  • validSeconds (int) – The amount of seconds the key has to be valid for

Returns:

None or value of key

getKeys(validSeconds=0)

Get keys for all contents

Parameters:

validSeconds (int) – valid time in seconds

Returns:

list

property lock

Return the lock. In practice, if the cache is shared among threads, it is a LockRing. Otherwise, it is just a mock object.

purgeAll(useLock=True)

Purge all entries CAUTION: useLock parameter should ALWAYS be True

Parameters:

useLock (bool) – use lock

purgeExpired(expiredInSeconds=0)

Purge all entries that are expired or will be expired in <expiredInSeconds>

Parameters:

expiredInSeconds (int) – expired time in a seconds

showContentsInString()

Return a human readable string to represent the contents

Returns:

str

class DIRAC.Core.Utilities.DictCache.MockLockRing

Bases: object

This mock class is just used to expose the acquire and release method

acquire(*args, **kwargs)

Really does nothing !

doNothing(*args, **kwargs)

Really does nothing !

release(*args, **kwargs)

Really does nothing !

class DIRAC.Core.Utilities.DictCache.ThreadLocalDict

Bases: _local

This class is just useful to have a mutable object (in this case, a dict) as a thread local Read the _threading_local docstring for more details.

Its purpose is to have a different cache per thread

__init__()

c’tor