| 152 | return False |
| 153 | |
| 154 | def set_cache(self, key, value, **kwargs): |
| 155 | # Handle the edge case where max_size_in_memory is 0 |
| 156 | if self.max_size_in_memory == 0: |
| 157 | return # Don't cache anything if max size is 0 |
| 158 | |
| 159 | # Always prune expired/outdated heap roots before inserting. |
| 160 | # This keeps expiration_heap bounded even when the live cache stays |
| 161 | # below max_size_in_memory and keys are reinserted after TTL expiry. |
| 162 | self.evict_cache() |
| 163 | if not self.check_value_size(value): |
| 164 | return |
| 165 | |
| 166 | self.cache_dict[key] = value |
| 167 | if self.allow_ttl_override(key): # if ttl is not set, set it to default ttl |
| 168 | if "ttl" in kwargs and kwargs["ttl"] is not None: |
| 169 | self.ttl_dict[key] = time.time() + float(kwargs["ttl"]) |
| 170 | heapq.heappush(self.expiration_heap, (self.ttl_dict[key], key)) |
| 171 | else: |
| 172 | self.ttl_dict[key] = time.time() + self.default_ttl |
| 173 | heapq.heappush(self.expiration_heap, (self.ttl_dict[key], key)) |
| 174 | |
| 175 | async def async_set_cache(self, key, value, **kwargs): |
| 176 | self.set_cache(key=key, value=value, **kwargs) |