Repository navigation
feat(storagecontrol): add Rapid Cache samples #2343
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c7a231a
feat(storagecontrol): add Rapid Cache samples
sahusneha2004 15c1b32
docs(storagecontrol): align rapid cache samples with reference integr…
sahusneha2004 9dd2ffc
fix(storagecontrol): resolve PR comments for rapid cache samples
sahusneha2004 6d26514
fix(storagecontrol): drop TTL from rapid cache create sample and simp…
sahusneha2004 c4ef063
Merge remote-tracking branch 'origin/main' into storagecontrol-rapid-…
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageControl; | ||
|
|
||
| # [START storage_control_create_rapid_cache] | ||
| use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; | ||
| use Google\Cloud\Storage\Control\V2\CreateRapidCacheRequest; | ||
| use Google\Cloud\Storage\Control\V2\RapidCache; | ||
|
|
||
| /** | ||
| * Creates a Rapid Cache instance. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| * @param string $zone The zone in which to create the Rapid Cache. The zone | ||
| * must be in the same region as the bucket. | ||
| * (e.g. 'us-central1-a') | ||
| */ | ||
| function create_rapid_cache(string $bucketName, string $zone): void | ||
| { | ||
| $storageControlClient = new StorageControlClient(); | ||
|
|
||
| // Set project to "_" to signify global bucket | ||
| $formattedName = $storageControlClient->bucketName('_', $bucketName); | ||
|
|
||
| // Rapid Cache Ultra requires a bucket with hierarchical namespace enabled. | ||
| $rapidCache = new RapidCache([ | ||
| 'zone' => $zone, | ||
| 'cache_type' => 'rapid-cache-ultra', | ||
| ]); | ||
|
|
||
| $request = new CreateRapidCacheRequest([ | ||
| 'parent' => $formattedName, | ||
| 'rapid_cache' => $rapidCache, | ||
| ]); | ||
|
|
||
| // Creating a Rapid Cache is a long-running operation. This sample blocks | ||
| // until it completes. | ||
| $operation = $storageControlClient->createRapidCache($request); | ||
| $operation->pollUntilComplete(); | ||
|
|
||
| /** @var RapidCache $rapidCacheResult */ | ||
| $rapidCacheResult = $operation->getResult(); | ||
| printf('Created Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->getName()); | ||
| } | ||
| # [END storage_control_create_rapid_cache] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageControl; | ||
|
|
||
| # [START storage_control_disable_rapid_cache] | ||
| use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; | ||
| use Google\Cloud\Storage\Control\V2\DisableRapidCacheRequest; | ||
|
|
||
| /** | ||
| * Disables a Rapid Cache instance. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| * @param string $rapidCacheId The ID of the Rapid Cache. | ||
| * (e.g. 'us-central1-a') | ||
| */ | ||
| function disable_rapid_cache(string $bucketName, string $rapidCacheId): void | ||
| { | ||
| $storageControlClient = new StorageControlClient(); | ||
|
|
||
| // Set project to "_" to signify global bucket | ||
| $formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId); | ||
|
|
||
| $request = new DisableRapidCacheRequest([ | ||
| 'name' => $formattedName, | ||
| ]); | ||
|
|
||
| // Disabling a Rapid Cache starts a long-running operation. The cache moves | ||
| // to the "disabled" state within seconds, but the operation only completes | ||
| // once the cache has been fully removed, which can take more than an hour. | ||
| // This sample does not wait for the operation to complete; use | ||
| // get_rapid_cache to check the cache state. | ||
| $operation = $storageControlClient->disableRapidCache($request); | ||
|
|
||
| printf('Disabling Rapid Cache: %s' . PHP_EOL, $formattedName); | ||
| printf('Operation: %s' . PHP_EOL, $operation->getName()); | ||
| } | ||
| # [END storage_control_disable_rapid_cache] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageControl; | ||
|
|
||
| # [START storage_control_get_rapid_cache] | ||
| use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; | ||
| use Google\Cloud\Storage\Control\V2\GetRapidCacheRequest; | ||
|
|
||
| /** | ||
| * Gets a Rapid Cache instance. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| * @param string $rapidCacheId The ID of the Rapid Cache. | ||
| * (e.g. 'us-central1-a') | ||
| */ | ||
| function get_rapid_cache(string $bucketName, string $rapidCacheId): void | ||
| { | ||
| $storageControlClient = new StorageControlClient(); | ||
|
|
||
| // Set project to "_" to signify global bucket | ||
| $formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId); | ||
|
|
||
| $request = new GetRapidCacheRequest([ | ||
| 'name' => $formattedName, | ||
| ]); | ||
|
|
||
| $response = $storageControlClient->getRapidCache($request); | ||
|
|
||
| printf('Got Rapid Cache: %s' . PHP_EOL, $response->getName()); | ||
| printf('Zone: %s' . PHP_EOL, $response->getZone()); | ||
| printf('State: %s' . PHP_EOL, $response->getState()); | ||
| } | ||
| # [END storage_control_get_rapid_cache] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageControl; | ||
|
|
||
| # [START storage_control_list_rapid_caches] | ||
| use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; | ||
| use Google\Cloud\Storage\Control\V2\ListRapidCachesRequest; | ||
|
|
||
| /** | ||
| * Lists Rapid Cache instances for a given bucket. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| */ | ||
| function list_rapid_caches(string $bucketName): void | ||
| { | ||
| $storageControlClient = new StorageControlClient(); | ||
|
|
||
| // Set project to "_" to signify global bucket | ||
| $formattedName = $storageControlClient->bucketName('_', $bucketName); | ||
|
|
||
| $request = new ListRapidCachesRequest([ | ||
| 'parent' => $formattedName, | ||
| ]); | ||
|
|
||
| $response = $storageControlClient->listRapidCaches($request); | ||
|
|
||
| // The iterator handles pagination automatically. | ||
| foreach ($response as $rapidCache) { | ||
| printf('Rapid Cache name: %s' . PHP_EOL, $rapidCache->getName()); | ||
| } | ||
| } | ||
| # [END storage_control_list_rapid_caches] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageControl; | ||
|
|
||
| # [START storage_control_update_rapid_cache] | ||
| use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; | ||
| use Google\Cloud\Storage\Control\V2\RapidCache; | ||
| use Google\Cloud\Storage\Control\V2\UpdateRapidCacheRequest; | ||
| use Google\Protobuf\Duration; | ||
| use Google\Protobuf\FieldMask; | ||
|
|
||
| /** | ||
| * Updates the time to live (TTL) of a Rapid Cache instance. | ||
| * | ||
| * @param string $bucketName The name of your Cloud Storage bucket. | ||
| * (e.g. 'my-bucket') | ||
| * @param string $rapidCacheId The ID of the Rapid Cache. | ||
| * (e.g. 'us-central1-a') | ||
| * @param int $ttlSeconds The new TTL for cache entries, in seconds. The TTL | ||
| * must be between 1 hour and 7 days. | ||
| * (e.g. 172800 for 2 days) | ||
| */ | ||
| function update_rapid_cache(string $bucketName, string $rapidCacheId, int $ttlSeconds): void | ||
| { | ||
| $storageControlClient = new StorageControlClient(); | ||
|
|
||
| // Set project to "_" to signify global bucket | ||
| $formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId); | ||
|
|
||
| $rapidCache = new RapidCache([ | ||
| 'name' => $formattedName, | ||
| 'ttl' => new Duration(['seconds' => $ttlSeconds]), | ||
| ]); | ||
|
|
||
| // Only the fields listed in the update mask are changed. | ||
| $updateMask = new FieldMask([ | ||
| 'paths' => ['ttl'], | ||
| ]); | ||
|
|
||
| $request = new UpdateRapidCacheRequest([ | ||
| 'rapid_cache' => $rapidCache, | ||
| 'update_mask' => $updateMask, | ||
| ]); | ||
|
|
||
| // Updating a Rapid Cache is a long-running operation. This sample blocks | ||
| // until it completes. | ||
| $operation = $storageControlClient->updateRapidCache($request); | ||
| $operation->pollUntilComplete(); | ||
|
|
||
| /** @var RapidCache $rapidCacheResult */ | ||
| $rapidCacheResult = $operation->getResult(); | ||
| printf('Updated Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->getName()); | ||
| printf('TTL: %d seconds' . PHP_EOL, $rapidCacheResult->getTtl()->getSeconds()); | ||
| } | ||
| # [END storage_control_update_rapid_cache] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.