Insert a key with the give value and charge. Return whether the insert was successful.
| 74 | // Insert a key with the give value and charge. Return whether the insert was |
| 75 | // successful. |
| 76 | bool Insert(int key, int value, int charge = 1) { |
| 77 | std::string key_str = EncodeInt(key); |
| 78 | std::string val_str = EncodeInt(value); |
| 79 | auto handle(cache_->Allocate(key_str, val_str.size(), charge)); |
| 80 | // This can be null if Allocate rejects something with this charge. |
| 81 | if (handle == nullptr) return false; |
| 82 | memcpy(cache_->MutableValue(&handle), val_str.data(), val_str.size()); |
| 83 | auto inserted_handle(cache_->Insert(std::move(handle), this)); |
| 84 | // Insert can fail and return nullptr |
| 85 | if (inserted_handle == nullptr) return false; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | void Erase(int key) { |
| 90 | cache_->Erase(EncodeInt(key)); |