Skip to content

Commit f19d196

Browse files
feat(storagecontrol): add Rapid Cache samples (#2343)
* feat(storagecontrol): add Rapid Cache samples Adds create, get, list, update and disable Rapid Cache samples for Storage Control, with tests. Bumps google/cloud-storage-control to ^1.12. b/482946642 [Generated-by: AI] * docs(storagecontrol): align rapid cache samples with reference integration test Set the explicit cache name (ID = zone), cache_type rapid-cache-ultra and ttl 24h on create; wait for the disable LRO; tests assert running state, cache type, list membership and the updated ttl, and bound the disable wait. Aligns with googleapis/google-cloud-java#14529. b/482946642 [Generated-by: AI] * fix(storagecontrol): resolve PR comments for rapid cache samples - Rename rapidCacheTest to RapidCacheTest (PSR-1 StudlyCaps). - Guard TTL output with hasTtl() in update_rapid_cache. [Generated-by: AI] * fix(storagecontrol): drop TTL from rapid cache create sample and simplify test teardown - create_rapid_cache: set only name, zone and cache type (rapid-cache-ultra), matching create_anywhere_cache; TTL is left to update_rapid_cache. - create_rapid_cache: use $rapidCacheId, consistent with get/update/disable. - RapidCacheTest: rename $cacheDisabled to $cacheActive, flatten tearDownAfterClass into disableCacheQuietly/deleteBucketQuietly helpers, and use a constant for the updated TTL. [Generated-by: AI] --------- Co-authored-by: Brent Shaffer <betterbrent@google.com>
1 parent b0bbb88 commit f19d196

7 files changed

Lines changed: 578 additions & 1 deletion

File tree

‎storagecontrol/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-storage-control": "^1.9"
3+
"google/cloud-storage-control": "^1.12"
44
},
55
"require-dev": {
66
"google/cloud-storage": "^2.0"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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_create_rapid_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\CreateRapidCacheRequest;
30+
use Google\Cloud\Storage\Control\V2\RapidCache;
31+
32+
/**
33+
* Creates a Rapid Cache instance with only the required fields. Optional
34+
* settings use their defaults (for example, a 24-hour TTL) and can be
35+
* changed later with update_rapid_cache.
36+
*
37+
* @param string $bucketName The name of your Cloud Storage bucket.
38+
* (e.g. 'my-bucket')
39+
* @param string $zone The zone in which to create the Rapid Cache. The zone
40+
* must be in the same region as the bucket.
41+
* (e.g. 'us-central1-a')
42+
*/
43+
function create_rapid_cache(string $bucketName, string $zone): void
44+
{
45+
$storageControlClient = new StorageControlClient();
46+
47+
// Set project to "_" to signify global bucket
48+
$formattedParent = $storageControlClient->bucketName('_', $bucketName);
49+
50+
// The Rapid Cache ID must be the same as the zone.
51+
$rapidCacheId = $zone;
52+
$formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId);
53+
54+
// Rapid Cache Ultra requires a bucket with hierarchical namespace enabled.
55+
$rapidCache = new RapidCache([
56+
'name' => $formattedName,
57+
'zone' => $zone,
58+
'cache_type' => 'rapid-cache-ultra',
59+
]);
60+
61+
$request = new CreateRapidCacheRequest([
62+
'parent' => $formattedParent,
63+
'rapid_cache' => $rapidCache,
64+
]);
65+
66+
// Creating a Rapid Cache is a long-running operation. This sample blocks
67+
// until it completes. A totalPollTimeoutMillis of 0 waits with no time
68+
// limit, instead of the client library default of 5 minutes.
69+
$operation = $storageControlClient->createRapidCache($request);
70+
$operation->pollUntilComplete(['totalPollTimeoutMillis' => 0]);
71+
72+
if (!$operation->operationSucceeded()) {
73+
printf('Error: %s' . PHP_EOL, $operation->getError()->getMessage());
74+
return;
75+
}
76+
77+
/** @var RapidCache $rapidCacheResult */
78+
$rapidCacheResult = $operation->getResult();
79+
printf('Created Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->getName());
80+
printf('State: %s' . PHP_EOL, $rapidCacheResult->getState());
81+
}
82+
# [END storage_control_create_rapid_cache]
83+
84+
// The following 2 lines are only needed to run the samples
85+
require_once __DIR__ . '/../../testing/sample_helpers.php';
86+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_disable_rapid_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\DisableRapidCacheRequest;
30+
use Google\Cloud\Storage\Control\V2\RapidCache;
31+
32+
/**
33+
* Disables a Rapid Cache instance.
34+
*
35+
* @param string $bucketName The name of your Cloud Storage bucket.
36+
* (e.g. 'my-bucket')
37+
* @param string $rapidCacheId The ID of the Rapid Cache.
38+
* (e.g. 'us-central1-a')
39+
* @param int $timeoutMillis Optional. The maximum time to wait for the
40+
* operation to complete, in milliseconds. 0 waits until it completes.
41+
* (e.g. 600000 for 10 minutes)
42+
*/
43+
function disable_rapid_cache(string $bucketName, string $rapidCacheId, int $timeoutMillis = 0): void
44+
{
45+
$storageControlClient = new StorageControlClient();
46+
47+
// Set project to "_" to signify global bucket
48+
$formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId);
49+
50+
$request = new DisableRapidCacheRequest([
51+
'name' => $formattedName,
52+
]);
53+
54+
// Disabling a Rapid Cache is a long-running operation. This sample waits
55+
// for the long-running operation to complete. A totalPollTimeoutMillis of
56+
// 0 waits with no time limit.
57+
$operation = $storageControlClient->disableRapidCache($request);
58+
$completed = $operation->pollUntilComplete([
59+
'totalPollTimeoutMillis' => $timeoutMillis,
60+
]);
61+
62+
if (!$completed) {
63+
printf('Disable operation is still running: %s' . PHP_EOL, $operation->getName());
64+
return;
65+
}
66+
67+
if (!$operation->operationSucceeded()) {
68+
printf('Error: %s' . PHP_EOL, $operation->getError()->getMessage());
69+
return;
70+
}
71+
72+
/** @var RapidCache $rapidCacheResult */
73+
$rapidCacheResult = $operation->getResult();
74+
printf('Disabled Rapid Cache: %s' . PHP_EOL, $rapidCacheResult->getName());
75+
}
76+
# [END storage_control_disable_rapid_cache]
77+
78+
// The following 2 lines are only needed to run the samples
79+
require_once __DIR__ . '/../../testing/sample_helpers.php';
80+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_get_rapid_cache]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\GetRapidCacheRequest;
30+
31+
/**
32+
* Gets a Rapid Cache instance.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
* @param string $rapidCacheId The ID of the Rapid Cache.
37+
* (e.g. 'us-central1-a')
38+
*/
39+
function get_rapid_cache(string $bucketName, string $rapidCacheId): void
40+
{
41+
$storageControlClient = new StorageControlClient();
42+
43+
// Set project to "_" to signify global bucket
44+
$formattedName = $storageControlClient->rapidCacheName('_', $bucketName, $rapidCacheId);
45+
46+
$request = new GetRapidCacheRequest([
47+
'name' => $formattedName,
48+
]);
49+
50+
$response = $storageControlClient->getRapidCache($request);
51+
52+
printf('Got Rapid Cache: %s' . PHP_EOL, $response->getName());
53+
printf('Zone: %s' . PHP_EOL, $response->getZone());
54+
printf('Cache type: %s' . PHP_EOL, $response->getCacheType());
55+
printf('State: %s' . PHP_EOL, $response->getState());
56+
}
57+
# [END storage_control_get_rapid_cache]
58+
59+
// The following 2 lines are only needed to run the samples
60+
require_once __DIR__ . '/../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_list_rapid_caches]
28+
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
29+
use Google\Cloud\Storage\Control\V2\ListRapidCachesRequest;
30+
31+
/**
32+
* Lists Rapid Cache instances for a given bucket.
33+
*
34+
* @param string $bucketName The name of your Cloud Storage bucket.
35+
* (e.g. 'my-bucket')
36+
*/
37+
function list_rapid_caches(string $bucketName): void
38+
{
39+
$storageControlClient = new StorageControlClient();
40+
41+
// Set project to "_" to signify global bucket
42+
$formattedName = $storageControlClient->bucketName('_', $bucketName);
43+
44+
$request = new ListRapidCachesRequest([
45+
'parent' => $formattedName,
46+
]);
47+
48+
$response = $storageControlClient->listRapidCaches($request);
49+
50+
// The iterator handles pagination automatically.
51+
foreach ($response as $rapidCache) {
52+
printf('Rapid Cache name: %s' . PHP_EOL, $rapidCache->getName());
53+
}
54+
}
55+
# [END storage_control_list_rapid_caches]
56+
57+
// The following 2 lines are only needed to run the samples
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)