-
Notifications
You must be signed in to change notification settings - Fork 2.9k
docs(memorystore): added valkey session snippets #10002
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
7
commits into
GoogleCloudPlatform:main
Choose a base branch
from
invertase:@invertase/memorystore-valkey-session-snippets
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3d4d0a
docs(memorystore): added valkey session snippets
dackers86 031a5c6
chore: make adjustments based on feedback
CorieW b876d6d
refactor(memorystore-session-snippets): lint
cabljac d7513dd
chore(session): addiitonal updates to redis naming and imports
dackers86 9439bed
chore(session): additional updates to redis naming and imports
dackers86 4e1ea0c
chore(sessions): fix license
CorieW 30f61be
chore(sessions): fix license
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
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,117 @@ | ||
<?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> | ||
|
||
<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> |
68 changes: 68 additions & 0 deletions
68
memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreAddItemToBasket.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,68 @@ | ||
/* | ||
* 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 IMPLIED WARRANTIES 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 MemorystoreAddItemToBasket { | ||
|
||
/** 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; | ||
|
||
/** User ID for managing the basket. */ | ||
private static final String USER_ID = "USER_ID"; | ||
|
||
/** Item to be added to the user's basket. */ | ||
private static final String ITEM_ID = "ITEM_ID"; | ||
|
||
/** Set how many items to be added to the basket. */ | ||
private static final int ITEM_COUNT = 1; | ||
|
||
private MemorystoreAddItemToBasket() { | ||
// No-op constructor to prevent instantiation | ||
} | ||
|
||
/** | ||
* Adds an item to a user's basket in Memorystore. | ||
* | ||
* @param args command-line arguments | ||
*/ | ||
public static void main(final String[] args) { | ||
// Create a Jedis connection pool | ||
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT); | ||
Jedis jedis = pool.getResource()) { | ||
|
||
String basketKey = "basket:" + USER_ID; | ||
|
||
// Add items to the user's basket | ||
jedis.hincrBy(basketKey, ITEM_ID, ITEM_COUNT); | ||
System.out.printf("Added %d items to basket: %s%n", ITEM_COUNT, ITEM_ID); | ||
|
||
// Verify the item is in the basket | ||
boolean exists = jedis.hexists(basketKey, ITEM_ID); | ||
if (exists) { | ||
System.out.printf("Item successfully added: %s%n", ITEM_ID); | ||
} else { | ||
System.out.printf("Failed to add item: %s%n", ITEM_ID); | ||
} | ||
} catch (Exception e) { | ||
System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreClearBasket.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,60 @@ | ||
/* | ||
* 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 IMPLIED WARRANTIES 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 MemorystoreClearBasket { | ||
|
||
/** 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; | ||
|
||
/** User ID for managing the basket */ | ||
private static final String USER_ID = "USER_ID"; | ||
|
||
private MemorystoreClearBasket() { | ||
// No-op; won't be called | ||
} | ||
|
||
/** | ||
* Clears a user's basket in 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()) { | ||
String basketKey = "basket:" + USER_ID; | ||
|
||
// Delete the basket (remove all items) | ||
long deleted = jedis.del(basketKey); | ||
|
||
// Verify if the basket was cleared | ||
if (deleted > 0) { | ||
System.out.printf("Basket cleared successfully for user: %s%n", USER_ID); | ||
} else { | ||
System.out.printf("No basket found for user: %s%n", USER_ID); | ||
} | ||
} catch (Exception e) { | ||
System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLoginUser.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 IMPLIED WARRANTIES 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; | ||
|
||
import java.util.UUID; | ||
|
||
public final class MemorystoreLoginUser { | ||
|
||
/** 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; | ||
|
||
/** User ID for login */ | ||
private static final String USER_ID = "USER_ID"; | ||
|
||
/** Session expiration time in seconds (30 minutes) */ | ||
private static final int SESSION_TIMEOUT = 1800; | ||
|
||
private MemorystoreLoginUser() { | ||
// No-op; won't be called | ||
} | ||
|
||
/** | ||
* Logs in a user by creating a session in 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()) { | ||
|
||
// Generate a session token | ||
String sessionToken = UUID.randomUUID().toString(); | ||
|
||
// Store the session token in Redis with an expiration time | ||
jedis.setex(sessionToken, SESSION_TIMEOUT, USER_ID); | ||
System.out.printf("User %s logged in with session: %s%n", USER_ID, sessionToken); | ||
} catch (Exception e) { | ||
System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLogoutUser.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,58 @@ | ||
/* | ||
* 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 IMPLIED WARRANTIES 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 MemorystoreLogoutUser { | ||
|
||
/** 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; | ||
|
||
/** User ID token for logout */ | ||
private static final String TOKEN = "TOKEN"; | ||
|
||
private MemorystoreLogoutUser() { | ||
// No-op; won't be called | ||
} | ||
|
||
/** | ||
* Logs out a user by deleting their session from Memorystore. | ||
* | ||
* @param args command-line arguments | ||
*/ | ||
public static void main(final String[] args) { | ||
// Connect to the Memorystore instance | ||
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT); | ||
Jedis jedis = pool.getResource()) { | ||
|
||
// Check if the session exists | ||
if (!jedis.exists(TOKEN)) { | ||
dackers86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
System.out.printf("User %s is not logged in.%n", TOKEN); | ||
return; | ||
} | ||
|
||
// Remove the session from Redis | ||
jedis.del(TOKEN); | ||
System.out.printf("User %s has been logged out.%n", TOKEN); | ||
} catch (Exception e) { | ||
System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); | ||
} | ||
} | ||
} |
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.