1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FCP_CLIENT_CACHE_RESOURCE_CACHE_H_ 18 #define FCP_CLIENT_CACHE_RESOURCE_CACHE_H_ 19 20 #include <optional> 21 22 #include "google/protobuf/any.pb.h" 23 #include "absl/status/status.h" 24 #include "absl/status/statusor.h" 25 #include "absl/strings/cord.h" 26 27 namespace fcp { 28 namespace client { 29 namespace cache { 30 31 /** 32 * A ResourceCache is an interface for a cache that stores resources (entries) 33 * for a certain duration. A resource consists of an absl::Cord payload and 34 * accompanying metadata, keyed by a string ID. 35 */ 36 class ResourceCache { 37 public: 38 struct ResourceAndMetadata { 39 absl::Cord resource; 40 google::protobuf::Any metadata; 41 }; 42 43 virtual ~ResourceCache() = default; 44 45 // Stores resource`under key cache_id. Will be deleted after now + max_age. If 46 // cache_id already exists, the corresponding resource and metadata will be 47 // overwritten. Metadata will be returned along with the resource when Get() 48 // is called. A Resource is *not* guaranteed to be cached until now + max_age, 49 // implementations may choose to delete resources earlier if needed. Returns 50 // Ok on success On error, returns 51 // - INTERNAL - unexpected error. 52 // - INVALID_ARGUMENT - if max_age is in the past. 53 // - RESOURCE_EXHAUSTED - if the resource is too big too be cached. 54 virtual absl::Status Put(absl::string_view cache_id, 55 const absl::Cord& resource, 56 const google::protobuf::Any& metadata, 57 absl::Duration max_age) = 0; 58 59 // Returns resource along with caller-provided stored metadata. 60 // If max_age is set, the stored max_age for this cache_id will be updated. 61 // Returns Ok on success 62 // On error, returns 63 // - INTERNAL - unexpected error. 64 // - NOT_FOUND - if cache_id not in ResourceCache 65 virtual absl::StatusOr<ResourceAndMetadata> Get( 66 absl::string_view cache_id, std::optional<absl::Duration> max_age) = 0; 67 }; 68 69 } // namespace cache 70 } // namespace client 71 } // namespace fcp 72 73 #endif // FCP_CLIENT_CACHE_RESOURCE_CACHE_H_ 74