Repository navigation
docs(storage): add Rapid Cache samples #14886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ | |
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <version>26.78.0</version> | ||
| <version>26.90.0</version> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove changes from this file, we do not edit these files manually. |
||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
|
|
@@ -99,7 +99,7 @@ | |
| <!-- tests jars aren't in the bom, manually include the version here --> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-storage</artifactId> | ||
| <version>2.64.0</version> | ||
| <version>2.75.0</version> | ||
| <classifier>tests</classifier> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.control.v2; | ||
|
|
||
| // [START storage_control_create_rapid_cache] | ||
|
|
||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.protobuf.Duration; | ||
| import com.google.storage.control.v2.BucketName; | ||
| import com.google.storage.control.v2.CreateRapidCacheMetadata; | ||
| import com.google.storage.control.v2.CreateRapidCacheRequest; | ||
| import com.google.storage.control.v2.RapidCache; | ||
| import com.google.storage.control.v2.RapidCacheName; | ||
| import com.google.storage.control.v2.StorageControlClient; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public final class RapidCacheCreate { | ||
|
|
||
| public static void rapidCacheCreate(String bucketName, String zoneName) | ||
| throws InterruptedException, ExecutionException, IOException { | ||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The zone in which to create the Rapid Cache. The zone must be in the same region as the | ||
| // bucket. | ||
| // String zoneName = "us-central1-a"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this variable to |
||
|
|
||
| try (StorageControlClient storageControl = StorageControlClient.create()) { | ||
|
|
||
| // Rapid Cache Ultra requires a regional bucket with hierarchical namespace enabled. | ||
| RapidCache rapidCache = | ||
| RapidCache.newBuilder() | ||
| // The Rapid Cache ID must be the same as the zone. | ||
| .setName(RapidCacheName.format("_", bucketName, zoneName)) | ||
| .setZone(zoneName) | ||
| .setCacheType("rapid-cache-ultra") | ||
| // How long a cache entry can live (between 1 hour and 7 days). 86400s = 24 hours. | ||
| .setTtl(Duration.newBuilder().setSeconds(86400).build()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In AnywhereCacheCreate.java, AnywhereCacheCreate only sets the required fields (.setName(...).setZone(zoneName)) and leaves TTL configuration to AnywhereCacheUpdate. Let's follow the same here. |
||
| .build(); | ||
|
|
||
| CreateRapidCacheRequest request = | ||
| CreateRapidCacheRequest.newBuilder() | ||
| // Set project to "_" to signify globally scoped bucket | ||
| .setParent(BucketName.format("_", bucketName)) | ||
| .setRapidCache(rapidCache) | ||
| .build(); | ||
|
|
||
| // Start a long-running operation (LRO). | ||
| OperationFuture<RapidCache, CreateRapidCacheMetadata> operation = | ||
| storageControl.createRapidCacheAsync(request); | ||
|
|
||
| // Await the LRO's completion. | ||
| RapidCache createdRapidCache = operation.get(); | ||
| System.out.printf("Created Rapid Cache: %s%n", createdRapidCache.getName()); | ||
| System.out.printf("Cache type: %s%n", createdRapidCache.getCacheType()); | ||
| System.out.printf("State: %s%n", createdRapidCache.getState()); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_control_create_rapid_cache] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.control.v2; | ||
|
|
||
| // [START storage_control_disable_rapid_cache] | ||
|
|
||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.storage.control.v2.DisableRapidCacheMetadata; | ||
| import com.google.storage.control.v2.DisableRapidCacheRequest; | ||
| import com.google.storage.control.v2.RapidCache; | ||
| import com.google.storage.control.v2.RapidCacheName; | ||
| import com.google.storage.control.v2.StorageControlClient; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public final class RapidCacheDisable { | ||
|
|
||
| public static void rapidCacheDisable(String bucketName, String cacheId) | ||
| throws InterruptedException, ExecutionException, IOException { | ||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The ID of the Rapid Cache | ||
| // String cacheId = "your-rapid-cache-id"; | ||
|
|
||
| try (StorageControlClient storageControl = StorageControlClient.create()) { | ||
|
|
||
| // Set project to "_" to signify globally scoped bucket | ||
| String rapidCacheName = RapidCacheName.format("_", bucketName, cacheId); | ||
| DisableRapidCacheRequest request = | ||
| DisableRapidCacheRequest.newBuilder().setName(rapidCacheName).build(); | ||
|
|
||
| // Start a long-running operation (LRO). | ||
| OperationFuture<RapidCache, DisableRapidCacheMetadata> operation = | ||
| storageControl.disableRapidCacheAsync(request); | ||
|
|
||
| // Await the LRO's completion. The cache moves to the "disabled" state within seconds, but | ||
| // the operation only completes once the cache has been fully removed, which can take longer | ||
| // than the client's default polling timeout. If the wait times out, the cache is already | ||
| // disabled; use RapidCacheGet to check its state. | ||
| RapidCache disabledRapidCache = operation.get(); | ||
| System.out.printf("Disabled Rapid Cache: %s%n", disabledRapidCache.getName()); | ||
| System.out.printf("State: %s%n", disabledRapidCache.getState()); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_control_disable_rapid_cache] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.control.v2; | ||
|
|
||
| // [START storage_control_get_rapid_cache] | ||
|
|
||
| import com.google.storage.control.v2.GetRapidCacheRequest; | ||
| import com.google.storage.control.v2.RapidCache; | ||
| import com.google.storage.control.v2.RapidCacheName; | ||
| import com.google.storage.control.v2.StorageControlClient; | ||
| import java.io.IOException; | ||
|
|
||
| public final class RapidCacheGet { | ||
|
|
||
| public static void rapidCacheGet(String bucketName, String cacheId) throws IOException { | ||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The ID of the Rapid Cache | ||
| // String cacheId = "your-rapid-cache-id"; | ||
|
|
||
| try (StorageControlClient storageControl = StorageControlClient.create()) { | ||
|
|
||
| GetRapidCacheRequest request = | ||
| GetRapidCacheRequest.newBuilder() | ||
| // Set project to "_" to signify globally scoped bucket | ||
| .setName(RapidCacheName.format("_", bucketName, cacheId)) | ||
| .build(); | ||
|
|
||
| RapidCache rapidCache = storageControl.getRapidCache(request); | ||
|
|
||
| System.out.printf("Got Rapid Cache: %s%n", rapidCache.getName()); | ||
| System.out.printf("Zone: %s%n", rapidCache.getZone()); | ||
| System.out.printf("Cache type: %s%n", rapidCache.getCacheType()); | ||
| System.out.printf("State: %s%n", rapidCache.getState()); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_control_get_rapid_cache] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.control.v2; | ||
|
|
||
| // [START storage_control_list_rapid_caches] | ||
|
|
||
| import com.google.storage.control.v2.BucketName; | ||
| import com.google.storage.control.v2.ListRapidCachesRequest; | ||
| import com.google.storage.control.v2.RapidCache; | ||
| import com.google.storage.control.v2.StorageControlClient; | ||
| import com.google.storage.control.v2.StorageControlClient.ListRapidCachesPagedResponse; | ||
| import java.io.IOException; | ||
|
|
||
| public final class RapidCacheList { | ||
|
|
||
| public static void rapidCacheList(String bucketName) throws IOException { | ||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| try (StorageControlClient storageControl = StorageControlClient.create()) { | ||
|
|
||
| ListRapidCachesRequest request = | ||
| ListRapidCachesRequest.newBuilder() | ||
| // Set project to "_" to signify globally scoped bucket | ||
| .setParent(BucketName.format("_", bucketName)) | ||
| .build(); | ||
|
|
||
| // The paged response handles pagination automatically. | ||
| ListRapidCachesPagedResponse response = storageControl.listRapidCaches(request); | ||
| for (RapidCache rapidCache : response.iterateAll()) { | ||
| System.out.println(rapidCache.getName()); | ||
| } | ||
|
|
||
| System.out.printf("Listed Rapid Caches in bucket %s%n", bucketName); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_control_list_rapid_caches] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.control.v2; | ||
|
|
||
| // [START storage_control_update_rapid_cache] | ||
|
|
||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.protobuf.Duration; | ||
| import com.google.protobuf.FieldMask; | ||
| import com.google.storage.control.v2.RapidCache; | ||
| import com.google.storage.control.v2.RapidCacheName; | ||
| import com.google.storage.control.v2.StorageControlClient; | ||
| import com.google.storage.control.v2.UpdateRapidCacheMetadata; | ||
| import com.google.storage.control.v2.UpdateRapidCacheRequest; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public final class RapidCacheUpdate { | ||
|
|
||
| public static void rapidCacheUpdate(String bucketName, String cacheId, long ttlSeconds) | ||
| throws InterruptedException, ExecutionException, IOException { | ||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The ID of the Rapid Cache | ||
| // String cacheId = "your-rapid-cache-id"; | ||
|
|
||
| // The new time to live (TTL) for cache entries, in seconds. The TTL must be between 1 hour and | ||
| // 7 days. | ||
| // long ttlSeconds = 172800; // 2 days | ||
|
|
||
| try (StorageControlClient storageControl = StorageControlClient.create()) { | ||
|
|
||
| RapidCache pendingUpdate = | ||
| RapidCache.newBuilder() | ||
| // Set project to "_" to signify globally scoped bucket | ||
| .setName(RapidCacheName.format("_", bucketName, cacheId)) | ||
| .setTtl(Duration.newBuilder().setSeconds(ttlSeconds).build()) | ||
| .build(); | ||
|
|
||
| UpdateRapidCacheRequest request = | ||
| UpdateRapidCacheRequest.newBuilder() | ||
| .setRapidCache(pendingUpdate) | ||
| // Only the fields listed in the update mask are changed. | ||
| .setUpdateMask(FieldMask.newBuilder().addPaths("ttl").build()) | ||
| .build(); | ||
|
|
||
| // Start a long-running operation (LRO). | ||
| OperationFuture<RapidCache, UpdateRapidCacheMetadata> operation = | ||
| storageControl.updateRapidCacheAsync(request); | ||
|
|
||
| // Await the LRO's completion. | ||
| RapidCache updatedRapidCache = operation.get(); | ||
| System.out.printf("Updated Rapid Cache: %s%n", updatedRapidCache.getName()); | ||
| System.out.printf("TTL: %d seconds%n", updatedRapidCache.getTtl().getSeconds()); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_control_update_rapid_cache] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove changes in this file, we do not modify these files directly, they are autgenerated.