Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) 2018 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15 

16""" 

17Action Cache 

18============ 

19 

20Implements an in-memory action Cache 

21""" 

22 

23import logging 

24 

25from ..referencestorage.storage import ReferenceCache 

26from ...utils import get_hash_type 

27 

28 

29class ActionCache(ReferenceCache): 

30 

31 def __init__(self, storage, max_cached_refs, allow_updates=True, cache_failed_actions=True): 

32 """ Initialises a new ActionCache instance. 

33 

34 Args: 

35 storage (StorageABC): storage backend instance to be used. Passed to ReferenceCache 

36 max_cached_refs (int): maximum number of entries to be stored. Passed to ReferenceCache 

37 allow_updates (bool): allow the client to write to storage. Passed to ReferenceCache 

38 cache_failed_actions (bool): cache actions with non-zero exit codes. 

39 """ 

40 super().__init__(storage, max_cached_refs, allow_updates) 

41 

42 self.__logger = logging.getLogger(__name__) 

43 

44 self._instance_name = None 

45 

46 self._cache_failed_actions = cache_failed_actions 

47 

48 # --- Public API --- 

49 

50 @property 

51 def instance_name(self): 

52 return self._instance_name 

53 

54 @instance_name.setter 

55 def instance_name(self, instance_name): 

56 self._instance_name = instance_name 

57 

58 def hash_type(self): 

59 return get_hash_type() 

60 

61 def register_instance_with_server(self, instance_name, server): 

62 """Names and registers the action-cache instance with a given server.""" 

63 if self._instance_name is None: 

64 server.add_action_cache_instance(self, instance_name) 

65 

66 self._instance_name = instance_name 

67 

68 else: 

69 raise AssertionError("Instance already registered") 

70 

71 def get_action_result(self, action_digest): 

72 """Retrieves the cached result for an action.""" 

73 key = self._get_key(action_digest) 

74 

75 return self.get_action_reference(key) 

76 

77 def update_action_result(self, action_digest, action_result): 

78 """Stores in cache a result for an action.""" 

79 if self._cache_failed_actions or action_result.exit_code == 0: 

80 key = self._get_key(action_digest) 

81 

82 self.update_reference(key, action_result) 

83 

84 self.__logger.info( 

85 f"Result cached for action [{action_digest.hash}/{action_digest.size_bytes}]") 

86 

87 # --- Private API --- 

88 

89 def _get_key(self, action_digest): 

90 return (action_digest.hash, action_digest.size_bytes)