-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(memorystore): added valkey caching practical source-code samples #9986
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
dackers86
wants to merge
28
commits into
GoogleCloudPlatform:main
Choose a base branch
from
invertase:@invertase/memorystore-valkey-caching-practical-source-code
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 24 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
485ce4b
docs(memorystore): added valkey practical sourcecode samples
dackers86 6166c60
docs(memorystore): added appropriate Apache 2 headers
dackers86 bab6fcb
docs(memorystore): chnaged folder name from samples to snippets
dackers86 9ec47ba
chore(memorystore): updated caching snippet pom metadata
dackers86 f539fbf
chore(memorystore): feedback updates on the readme
dackers86 05d02e6
chore(memorystore): moved configurable variables into the class file,…
dackers86 0b14b58
fix(memorystore): fixed caching POM error
dackers86 e6c53d6
docs(caching): updated read item snippet variables
dackers86 eed2313
memorystore(caching): minor comment update
dackers86 99728be
fix(memorystore): added try-with-resources to all snippets
dackers86 c141400
fix(memorystore): ensure the cached item has been successfully cached…
dackers86 626c3bf
docs(memorystore): readme updated to include terminal guides on setti…
dackers86 5c9fe42
chore(memorystore): headers updated for the current year
dackers86 0384dd8
chore(memorstore): updated write item variables
dackers86 f31a379
chore(memorstore): added package-info.java
dackers86 26a66ad
chore(memorystore): fixed linting on delete item example
dackers86 580628a
chore(memorystore): additional updates
dackers86 3a922b8
fix(memorystore): fixed linting errors on the read item snippet
dackers86 828f9c2
fix(memorystore): fixed linting errors on the ttl item snippet
dackers86 478abb9
fix(memorystore): fixed linting errors on the write item snippet
dackers86 765a078
chore(memorystore): added header to package-info.java
dackers86 9bf2a14
chore(memorstore-caching-snippets): lint
cabljac 6eefe92
docs(caching): updated valkey cli cluster link
dackers86 2489c16
docs(caching): updated valkey cli cluster link
dackers86 71f4934
chore(caching): fix license
CorieW 0ee8173
chore(caching): fix license
dackers86 439d4cf
chore(caching): fix spelling error
dackers86 2c35901
chore(caching): fix spelling error
dackers86 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
Empty file.
Empty file.
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,43 @@ | ||
# Caching Samples | ||
|
||
This folder contains a samples in form of stand-alone snippets that demonstrate useful scenarios. | ||
|
||
## Prerequiites | ||
|
||
Ensure the following setup is in place. | ||
|
||
### Java | ||
|
||
You must have java installed locally on your machinee. Run `java --version` to check if this is available. | ||
|
||
### Memorystore for Valkey Instance | ||
|
||
A working instance of Memorystore for Valkey must be available. You can run the [Valkey CLI](https://valkey.io/topics/cluster-tutorial/#create-a-valkey-cluster) for a local instance, or create an instance through the [GCP Platform](https://console.cloud.google.com/memorystore/valkey/instances?). | ||
|
||
To setup a live instance, create a new Memorystore instance through the GCloud CLI using the following | ||
|
||
```bash | ||
gcloud redis instances create myinstance --size=2 --region=LOCATION --redis-version=redis_6_x | ||
``` | ||
|
||
Altrernativley, run a local instance through the Valkey CLI | ||
|
||
```bash | ||
valkey-cli | ||
``` | ||
|
||
## Running the sample code | ||
|
||
Each example contains instructions on any prerequiite configuration. | ||
dackers86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Compile the app through Maven (optional) | ||
|
||
```bash | ||
mvn compile | ||
``` | ||
|
||
## Run the sample code | ||
|
||
```bash | ||
mvn exec:java -Dexec.mainClass=MemorystoreTtlItem #Replace the main class as needed | ||
``` |
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,127 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
Copyright 2020 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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example.memorystore</groupId> | ||
<artifactId>caching</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>Google Cloud Memorystore Sample</name> | ||
<url>http://www.example.com</url> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.2.0</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.release>11</maven.compiler.release> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit</groupId> | ||
<artifactId>junit-bom</artifactId> | ||
<version>5.11.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- Jedis Redis client library --> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>4.3.1</version> | ||
</dependency> | ||
|
||
<!-- Google Cloud Client Libraries --> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-core</artifactId> | ||
<version>2.16.0</version> | ||
</dependency> | ||
|
||
<!-- Unit testing libraries --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.4.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.3.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.13.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.3.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.4.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>3.1.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>3.1.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.12.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.6.1</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
61 changes: 61 additions & 0 deletions
61
memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreDeleteItem.java
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 @@ | ||
/* | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
public final class MemorystoreDeleteItem { | ||
|
||
/** Replace the instance ID of the Memorystore instance. */ | ||
private static final String INSTANCE_ID = "INSTANCE_ID"; | ||
|
||
/** Replace the Memorystore port, if not the default port. */ | ||
private static final int PORT = 6379; | ||
|
||
/** Replace the id of the item to delete from Memorystore. */ | ||
private static final String ITEM_ID = "ITEM_ID"; | ||
|
||
private MemorystoreDeleteItem() { | ||
// No-op; won't be called | ||
} | ||
|
||
/** | ||
* Deletes an item from Memorystore. | ||
* | ||
* @param args command-line arguments | ||
*/ | ||
public static void main(final String[] args) { | ||
|
||
// Connect to the Memorystore instance | ||
JedisPool pool = new JedisPool(INSTANCE_ID, PORT); | ||
|
||
try (Jedis jedis = pool.getResource()) { | ||
|
||
// Attempt to delete the item | ||
Long result = jedis.del(ITEM_ID); | ||
|
||
// Check if the item was successfully deleted | ||
if (result > 0) { | ||
System.out.println("Deleted item: " + ITEM_ID); | ||
} | ||
|
||
// Print the cached item not found | ||
if (result == 0) { | ||
System.out.printf("No cached item found: %s%n", ITEM_ID); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreReadItem.java
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 @@ | ||
/* | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
public final class MemorystoreReadItem { | ||
|
||
/** Replace the instance ID of the Memorystore instance. */ | ||
private static final String INSTANCE_ID = "INSTANCE_ID"; | ||
|
||
/** If valid, replace the port number of the Memorystore instance. */ | ||
private static final int PORT = 6379; | ||
|
||
/** Replace, the ID of the item to delete from the cache. */ | ||
private static final String ITEM_ID = "ITEM_ID"; | ||
|
||
private MemorystoreReadItem() { | ||
// No-op; won't be called | ||
} | ||
|
||
/** | ||
* Reads an item from Memorystore. | ||
* | ||
* @param args command-line arguments | ||
*/ | ||
public static void main(final String[] args) { | ||
|
||
// Connect to the Memorystore instance | ||
JedisPool pool = new JedisPool(INSTANCE_ID, PORT); | ||
|
||
try (Jedis jedis = pool.getResource()) { | ||
|
||
// Attempt to read the cached item | ||
String cachedItem = jedis.get(ITEM_ID); | ||
|
||
// Print the cached item if found | ||
if (cachedItem != null) { | ||
System.out.println("Cached item: " + cachedItem); | ||
} | ||
|
||
// Print the cached item not found | ||
if (cachedItem == null) { | ||
System.out.printf("No cached item found: %s%n", ITEM_ID); | ||
} | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreTTLItem.java
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,89 @@ | ||
/* | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
public final class MemorystoreTtlItem { | ||
|
||
/** Replace the Memorystore instance id. */ | ||
private static final String INSTANCE_ID = "INSTANCE_ID"; | ||
|
||
/** Replace the Memorystore port, if not the default port. */ | ||
private static final int PORT = 6379; | ||
|
||
/** Replace the id of the item to write to Memorystore. */ | ||
private static final String ITEM_ID = "ITEM_ID"; | ||
|
||
/** Replace the id of the item to write to Memorystore. */ | ||
private static final String ITEM_VALUE = "ITEM_VALUE"; | ||
|
||
/** Set the initial wait time for checking the cache. */ | ||
private static final int INITIAL_WAIT_TIME = 5000; | ||
|
||
/** Set the final wait time for checking the cache. */ | ||
private static final int FINAL_WAIT_TIME = 6000; | ||
|
||
private MemorystoreTtlItem() { | ||
// No-op; won't be called | ||
} | ||
|
||
/** | ||
* Writes to Memorystore with a Time-to-live(TTL) value. | ||
* | ||
* @param args command-line arguments | ||
* @throws InterruptedException if the thread sleep is interrupted | ||
*/ | ||
public static void main(final String[] args) throws InterruptedException { | ||
|
||
// Connect to the Memorystore instance | ||
JedisPool pool = new JedisPool(INSTANCE_ID, PORT); | ||
|
||
try (Jedis jedis = pool.getResource()) { | ||
|
||
// Set a TTL of 10 seconds during entry creation | ||
final int ttlSeconds = 10; | ||
jedis.setex(ITEM_ID, ttlSeconds, ITEM_VALUE); | ||
|
||
// Print out the cached item details | ||
System.out.printf( | ||
"Item cached with ID: %s and value: %s for %ds%n", | ||
ITEM_ID, ITEM_VALUE, ttlSeconds); | ||
|
||
// Wait for 5 seconds to demonstrate the TTL countdown | ||
System.out.println("Waiting for 5 seconds..."); | ||
Thread.sleep(INITIAL_WAIT_TIME); | ||
|
||
// Retrieve and print the remaining TTL | ||
Long remainingTtl = jedis.ttl(ITEM_ID); | ||
System.out.printf("Remaining TTL %s: %ds%n", ITEM_ID, remainingTtl); | ||
|
||
// Wait for another 6 seconds to demonstrate TTL expiry | ||
System.out.println("Waiting for 6s for TTL expiry..."); | ||
Thread.sleep(FINAL_WAIT_TIME); | ||
|
||
// Check the remaining TTL and item existence | ||
remainingTtl = jedis.ttl(ITEM_ID); | ||
if (remainingTtl < 0) { | ||
System.out.printf("Item with ID %s has expired.", ITEM_ID); | ||
} | ||
|
||
if (remainingTtl >= 0) { | ||
System.out.printf("Found Item %s", ITEM_ID, remainingTtl); | ||
} | ||
} | ||
} | ||
} |
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.