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
Draft
sahusneha2004
wants to merge
2
commits into
main
Choose a base branch
from
storagecontrol-rapid-cache-samples-php
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,86 @@ | ||
| <?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; | ||
| use Google\Protobuf\Duration; | ||
|
|
||
| /** | ||
| * 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 | ||
| $formattedParent = $storageControlClient->bucketName('_', $bucketName); | ||
|
|
||
| // The Rapid Cache ID must be the same as the zone. | ||
| $formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $zone); | ||
|
|
||
| // Rapid Cache Ultra requires a bucket with hierarchical namespace enabled. | ||
| $rapidCache = new RapidCache([ | ||
| 'name' => $formattedName, | ||
| 'zone' => $zone, | ||
| 'cache_type' => 'rapid-cache-ultra', | ||
| // Time to live (TTL) for cache entries: 86400 seconds (24 hours). | ||
| 'ttl' => new Duration(['seconds' => 86400]), | ||
| ]); | ||
|
|
||
| $request = new CreateRapidCacheRequest([ | ||
| 'parent' => $formattedParent, | ||
| 'rapid_cache' => $rapidCache, | ||
| ]); | ||
|
|
||
| // Creating a Rapid Cache is a long-running operation. This sample blocks | ||
| // until it completes. A totalPollTimeoutMillis of 0 waits with no time | ||
| // limit, instead of the client library default of 5 minutes. | ||
| $operation = $storageControlClient->createRapidCache($request); | ||
| $operation->pollUntilComplete(['totalPollTimeoutMillis' => 0]); | ||
|
|
||
| if (!$operation->operationSucceeded()) { | ||
| printf('Error: %s' . PHP_EOL, $operation->getError()->getMessage()); | ||
| return; | ||
| } | ||
|
|
||
| /** @var RapidCache $rapidCacheResult */ | ||
| $rapidCacheResult = $operation->getResult(); | ||
| printf('Created Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->getName()); | ||
| printf('State: %s' . PHP_EOL, $rapidCacheResult->getState()); | ||
| } | ||
| # [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,80 @@ | ||
| <?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; | ||
| use Google\Cloud\Storage\Control\V2\RapidCache; | ||
|
|
||
| /** | ||
| * 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') | ||
| * @param int $timeoutMillis Optional. The maximum time to wait for the | ||
| * operation to complete, in milliseconds. 0 waits until it completes. | ||
| * (e.g. 600000 for 10 minutes) | ||
| */ | ||
| function disable_rapid_cache(string $bucketName, string $rapidCacheId, int $timeoutMillis = 0): 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 is a long-running operation. This sample waits | ||
| // for the long-running operation to complete. A totalPollTimeoutMillis of | ||
| // 0 waits with no time limit. | ||
| $operation = $storageControlClient->disableRapidCache($request); | ||
| $completed = $operation->pollUntilComplete([ | ||
| 'totalPollTimeoutMillis' => $timeoutMillis, | ||
| ]); | ||
|
|
||
| if (!$completed) { | ||
| printf('Disable operation is still running: %s' . PHP_EOL, $operation->getName()); | ||
| return; | ||
| } | ||
|
|
||
| if (!$operation->operationSucceeded()) { | ||
| printf('Error: %s' . PHP_EOL, $operation->getError()->getMessage()); | ||
| return; | ||
| } | ||
|
|
||
| /** @var RapidCache $rapidCacheResult */ | ||
| $rapidCacheResult = $operation->getResult(); | ||
| printf('Disabled Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->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,61 @@ | ||
| <?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('Cache type: %s' . PHP_EOL, $response->getCacheType()); | ||
| 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,87 @@ | ||
| <?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. A totalPollTimeoutMillis of 0 waits with no time | ||
| // limit, instead of the client library default of 5 minutes. | ||
| $operation = $storageControlClient->updateRapidCache($request); | ||
| $operation->pollUntilComplete(['totalPollTimeoutMillis' => 0]); | ||
|
|
||
| if (!$operation->operationSucceeded()) { | ||
| printf('Error: %s' . PHP_EOL, $operation->getError()->getMessage()); | ||
| return; | ||
| } | ||
|
|
||
| /** @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.
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.
To prevent a potential null pointer dereference (fatal error) if the server does not return the TTL in the response, it is safer to check if the TTL is set before calling getSeconds(). In Protobuf PHP, you can use the hasTtl() method to verify if the message field is populated.