Save value for the given key. :param key: Must be a ``/`` separated value. Usually the first name is the name of your plugin or your application. :param value: Must be of any combination of basic python types, including nested types li
(self, key: str, value: object)
| 163 | return default |
| 164 | |
| 165 | def set(self, key: str, value: object) -> None: |
| 166 | """Save value for the given key. |
| 167 | |
| 168 | :param key: |
| 169 | Must be a ``/`` separated value. Usually the first |
| 170 | name is the name of your plugin or your application. |
| 171 | :param value: |
| 172 | Must be of any combination of basic python types, |
| 173 | including nested types like lists of dictionaries. |
| 174 | """ |
| 175 | path = self._getvaluepath(key) |
| 176 | try: |
| 177 | if path.parent.is_dir(): |
| 178 | cache_dir_exists_already = True |
| 179 | else: |
| 180 | cache_dir_exists_already = self._cachedir.exists() |
| 181 | path.parent.mkdir(exist_ok=True, parents=True) |
| 182 | except OSError: |
| 183 | self.warn("could not create cache path {path}", path=path, _ispytest=True) |
| 184 | return |
| 185 | if not cache_dir_exists_already: |
| 186 | self._ensure_supporting_files() |
| 187 | data = json.dumps(value, indent=2) |
| 188 | try: |
| 189 | f = path.open("w") |
| 190 | except OSError: |
| 191 | self.warn("cache could not write path {path}", path=path, _ispytest=True) |
| 192 | else: |
| 193 | with f: |
| 194 | f.write(data) |
| 195 | |
| 196 | def _ensure_supporting_files(self) -> None: |
| 197 | """Create supporting files in the cache dir that are not really part of the cache.""" |