diff --git a/memorystore/valkey/caching/snippets/.mvn/jvm.config b/memorystore/valkey/caching/snippets/.mvn/jvm.config new file mode 100644 index 00000000000..e69de29bb2d diff --git a/memorystore/valkey/caching/snippets/.mvn/maven.config b/memorystore/valkey/caching/snippets/.mvn/maven.config new file mode 100644 index 00000000000..e69de29bb2d diff --git a/memorystore/valkey/caching/snippets/README.md b/memorystore/valkey/caching/snippets/README.md new file mode 100644 index 00000000000..af13b829f4b --- /dev/null +++ b/memorystore/valkey/caching/snippets/README.md @@ -0,0 +1,43 @@ +# Caching Samples + +This folder contains a samples in form of stand-alone snippets that demonstrate useful scenarios. + +## Prerequisites + +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 prerequisite configuration. + +## 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 +``` diff --git a/memorystore/valkey/caching/snippets/pom.xml b/memorystore/valkey/caching/snippets/pom.xml new file mode 100644 index 00000000000..320e63a8d0a --- /dev/null +++ b/memorystore/valkey/caching/snippets/pom.xml @@ -0,0 +1,127 @@ + + + + + + 4.0.0 + + com.example.memorystore + caching + 1.0-SNAPSHOT + + Google Cloud Memorystore Sample + http://www.example.com + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + UTF-8 + 11 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + + redis.clients + jedis + 4.3.1 + + + + + com.google.cloud + google-cloud-core + 2.16.0 + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + maven-clean-plugin + 3.4.0 + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + diff --git a/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreDeleteItem.java b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreDeleteItem.java new file mode 100644 index 00000000000..f14480ec150 --- /dev/null +++ b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreDeleteItem.java @@ -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); + } + } + } +} diff --git a/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreReadItem.java b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreReadItem.java new file mode 100644 index 00000000000..f315ab797f4 --- /dev/null +++ b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreReadItem.java @@ -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); + } + } + } +} diff --git a/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreTTLItem.java b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreTTLItem.java new file mode 100644 index 00000000000..bc1d1dcb81b --- /dev/null +++ b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreTTLItem.java @@ -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); + } + } + } +} diff --git a/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreWriteItem.java b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreWriteItem.java new file mode 100644 index 00000000000..b1ec34aae66 --- /dev/null +++ b/memorystore/valkey/caching/snippets/src/main/java/samples/MemorystoreWriteItem.java @@ -0,0 +1,69 @@ +/* + * 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 MemorystoreWriteItem { + + /** 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"; + + private MemorystoreWriteItem() { + // No-op; won't be called + } + + /** + * Writes to Memorystore before verifying the item exists. + * + * @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()) { + + // Write the item to the cache. + System.out.printf("Caching item %s %s%n", ITEM_ID, ITEM_VALUE); + jedis.set(ITEM_ID, ITEM_VALUE); + + // Verify the cached item + System.out.println("Verifying cache."); + String cachedItem = jedis.get(ITEM_ID); + + // Print the cached item if found + if (cachedItem != null) { + System.out.printf("Found cached item: %s%n", cachedItem); + } + + // Print the cached item not found + if (cachedItem == null) { + System.out.printf("No cached item found: %s%n", ITEM_ID); + } + } + } +} diff --git a/memorystore/valkey/caching/snippets/src/main/java/samples/package-info.java b/memorystore/valkey/caching/snippets/src/main/java/samples/package-info.java new file mode 100644 index 00000000000..429a0d1a6ff --- /dev/null +++ b/memorystore/valkey/caching/snippets/src/main/java/samples/package-info.java @@ -0,0 +1,27 @@ +/* + * 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. + */ + +/** + * This package demonstrates Google Cloud Memorystore for Redis. It includes: + * + * + */ +package com.example.memorystore;