Skip to content
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

feat(memorystore): added caching practical source-code #2

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions memorystore/valkey/caching/samples/REAMDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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/cli/) for a local instance, or create an instance through the [GCP Platform](https://console.cloud.google.com/memorystore/valkey/instances?).

## Running the sample code

Each example contains instructions on any prerequiite 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
```
100 changes: 100 additions & 0 deletions memorystore/valkey/caching/samples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>samples</groupId>
<artifactId>samples</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Google Cloud Memorystore Sample</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* This code snippet is part of a tutorial on how to use Memorystore for Valkey.
*
* <p>See https://cloud.google.com/memorystore/docs/valkey/create-instances before running the code
* snippet.
*
* <p>Prerequisites: 1. A running Memorystore for Valkey instance in Google Cloud. 2. Your client
* must run in the same VPC network as the Memorystore instance.
*
* <p>Replace "INSTANCE_ID" with the private IP of your Memorystore instance. Replace "ITEM_ID" with
* the key to be deleted from the cache.
*/
import redis.clients.jedis.Jedis;

public class MemorystoreDeleteItem {

public static void main(String[] args) {
/** Connect to your Memorystore for Valkey instance */
Jedis jedis = new Jedis("127.0.0.1", 6379);

/** Replace with the item ID to delete from the cache */
String itemId = "foo";

/** Delete the item from the cache */
Long result = jedis.del(itemId);

/** Check if any items have been successfully deleted */
if (result > 0) {
System.out.println("Successfully deleted item with ID: " + itemId);
}

/* Print out that no item has been found for deletion */
if (result == 0) {
System.out.println("No item found with ID: " + itemId);
}

/** Close the Redis connection */
jedis.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This code snippet demonstrates how to read an item from Google Cloud Memorystore for Redis.
*
* <p>See https://cloud.google.com/memorystore/docs/valkey/create-instances before running the code
* snippet.
*
* <p>Prerequisites: 1. A running Memorystore for Redis instance in Google Cloud. 2. An item already
* cached in the Redis instance with a known ID.
*
* <p>Replace "INSTANCE_ID" with the private IP of your Memorystore instance. Replace "ITEM_ID" with
* an actual cached item ID.
*/
import redis.clients.jedis.Jedis;

public class MemorystoreReadItem {

public static void main(String[] args) {
/** Connect to your Memorystore for Valkey instance */
Jedis jedis = new Jedis("127.0.0.1", 6379);

/** Replace with an actual cached item ID */
String itemId = "foo";

/** Read the cached item */
String cachedItem = jedis.get(itemId);

/* If found, print out the cached item */
if (cachedItem != null) {
System.out.println("Cached item: " + cachedItem);
}

/** If no item found, print a message */
if (cachedItem == null) {
System.out.println("No cached item found with ID: " + itemId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* This code snippet is part of a tutorial on how to use Memorystore for Valkey.
*
* <p>See https://cloud.google.com/memorystore/docs/valkey/create-instances before running the code
* snippet.
*
* <p>Prerequisites: 1. A running Memorystore for Valkey instance in Google Cloud. 2. Your client
* must run in the same VPC network as the Memorystore instance.
*
* <p>Replace "INSTANCE_ID" with the private IP of your Memorystore instance. Replace "ITEM_ID" and
* "ITEM_VALUE" with the key and value to cache.
*/
import redis.clients.jedis.Jedis;

public class MemorystoreTTLItem {

public static void main(String[] args) throws InterruptedException {
/** Connect to your Memorystore for Valkey instance */
Jedis jedis = new Jedis("127.0.0.1", 6379);

/** Replace with the item ID and value to cache */
String itemId = "foo";
String itemValue = "bar";

/** Set a TTL of 10 seconds during entry creation */
int ttlSeconds = 10;

/** Create a new cached item with a TTL value */
jedis.setex(itemId, ttlSeconds, itemValue);

/** Print out the cached item details */
System.out.println(
"Item cached with ID: " + itemId + " and value: " + itemValue + " for " + ttlSeconds + "s");

/** Wait for 5 seconds */
System.out.println("Waiting for 5 seconds...");
Thread.sleep(5000);

/** Retrieve the remaining TTL */
Long remainingTTL = jedis.ttl(itemId);

/** Find the cached item, and print out the remanining expiry */
String cachedItem = jedis.get(itemId);

/** Print out the item id with remaining TTL value */
System.out.println("Remaining TTL for item " + itemId + ": " + remainingTTL + "s");

/** Wait for another 6 seconds to demonstrate TTL expiry */
System.out.println("Waiting for 6 seconds for expiry...");
Thread.sleep(6000);

/** Retrieve the remaining TTL or check item existence */
remainingTTL = jedis.ttl(itemId);

/** If TTL is less than 0, print a message indicating expiration */
if (remainingTTL < 0) {
System.out.println("Item with ID " + itemId + " has expired and is no longer available.");
}

/** Close the Redis connection */
jedis.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* This code snippet is part of a tutorial on how to use Memorystore for Redis.
*
* <p>See https://cloud.google.com/memorystore/docs/valkey/create-instances before running the code
* snippet.
*
* <p>Prerequisites: 1. A running Memorystore for Redis instance in Google Cloud.
*
* <p>Replace "INSTANCE_ID" with the private IP of your Memorystore instance. Replace "ITEM_ID" and
* "ITEM_VALUE" with the key and value to be cached.
*/
import redis.clients.jedis.Jedis;

public class MemorystoreWriteItem {

public static void main(String[] args) {
/** Connect to the Memorystore Redis instance */
Jedis jedis = new Jedis("127.0.0.1", 6379);

/** Replace with the item ID and value to cache */
String itemId = "foo";
String itemValue = "bar";

/** Write the item to the cache */
jedis.set(itemId, itemValue);

/** Print out the cached result */
System.out.println("Item cached with ID: " + itemId + " and value: " + itemValue);

/** Close the connection */
jedis.close();
}
}