|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2026 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * For instructions on how to run the full sample: |
| 21 | + * |
| 22 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Google\Cloud\Samples\StorageControl; |
| 26 | + |
| 27 | +# [START storage_control_update_rapid_cache] |
| 28 | +use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; |
| 29 | +use Google\Cloud\Storage\Control\V2\RapidCache; |
| 30 | +use Google\Cloud\Storage\Control\V2\UpdateRapidCacheRequest; |
| 31 | +use Google\Protobuf\Duration; |
| 32 | +use Google\Protobuf\FieldMask; |
| 33 | + |
| 34 | +/** |
| 35 | + * Updates the time to live (TTL) of a Rapid Cache instance. |
| 36 | + * |
| 37 | + * @param string $bucketName The name of your Cloud Storage bucket. |
| 38 | + * (e.g. 'my-bucket') |
| 39 | + * @param string $rapidCacheId The ID of the Rapid Cache. |
| 40 | + * (e.g. 'us-central1-a') |
| 41 | + * @param int $ttlSeconds The new TTL for cache entries, in seconds. The TTL |
| 42 | + * must be between 1 hour and 7 days. |
| 43 | + * (e.g. 172800 for 2 days) |
| 44 | + */ |
| 45 | +function update_rapid_cache(string $bucketName, string $rapidCacheId, int $ttlSeconds): void |
| 46 | +{ |
| 47 | + $storageControlClient = new StorageControlClient(); |
| 48 | + |
| 49 | + // Set project to "_" to signify global bucket |
| 50 | + $formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId); |
| 51 | + |
| 52 | + $rapidCache = new RapidCache([ |
| 53 | + 'name' => $formattedName, |
| 54 | + 'ttl' => new Duration(['seconds' => $ttlSeconds]), |
| 55 | + ]); |
| 56 | + |
| 57 | + // Only the fields listed in the update mask are changed. |
| 58 | + $updateMask = new FieldMask([ |
| 59 | + 'paths' => ['ttl'], |
| 60 | + ]); |
| 61 | + |
| 62 | + $request = new UpdateRapidCacheRequest([ |
| 63 | + 'rapid_cache' => $rapidCache, |
| 64 | + 'update_mask' => $updateMask, |
| 65 | + ]); |
| 66 | + |
| 67 | + // Updating a Rapid Cache is a long-running operation. This sample blocks |
| 68 | + // until it completes. A totalPollTimeoutMillis of 0 waits with no time |
| 69 | + // limit, instead of the client library default of 5 minutes. |
| 70 | + $operation = $storageControlClient->updateRapidCache($request); |
| 71 | + $operation->pollUntilComplete(['totalPollTimeoutMillis' => 0]); |
| 72 | + |
| 73 | + if (!$operation->operationSucceeded()) { |
| 74 | + printf('Error: %s' . PHP_EOL, $operation->getError()->getMessage()); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + /** @var RapidCache $rapidCacheResult */ |
| 79 | + $rapidCacheResult = $operation->getResult(); |
| 80 | + printf('Updated Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->getName()); |
| 81 | + if ($rapidCacheResult->hasTtl()) { |
| 82 | + printf('TTL: %d seconds' . PHP_EOL, $rapidCacheResult->getTtl()->getSeconds()); |
| 83 | + } |
| 84 | +} |
| 85 | +# [END storage_control_update_rapid_cache] |
| 86 | + |
| 87 | +// The following 2 lines are only needed to run the samples |
| 88 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 89 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments