diff --git a/memorystore/valkey/session/snippets/pom.xml b/memorystore/valkey/session/snippets/pom.xml new file mode 100644 index 00000000000..a22714f64b4 --- /dev/null +++ b/memorystore/valkey/session/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 + + + UTF-8 + 11 + + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + + + 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 + + + + + \ No newline at end of file diff --git a/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreAddItemToBasket.java b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreAddItemToBasket.java new file mode 100644 index 00000000000..c905369758d --- /dev/null +++ b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreAddItemToBasket.java @@ -0,0 +1,70 @@ +/* +* 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. +*/ + +package samples; + +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 Valkey: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreClearBasket.java b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreClearBasket.java new file mode 100644 index 00000000000..015062d70bd --- /dev/null +++ b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreClearBasket.java @@ -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 CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package samples; + +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 + try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + Jedis jedis = pool.getResource()) { + + // 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 Valkey: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLoginUser.java b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLoginUser.java new file mode 100644 index 00000000000..a116b39c1f8 --- /dev/null +++ b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLoginUser.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. +*/ + +package samples; + +import java.util.UUID; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +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 + try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + Jedis jedis = pool.getResource()) { + + // Generate a session token + String sessionToken = UUID.randomUUID().toString(); + + // Store the session token in Valkey 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 Valkey: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLogoutUser.java b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLogoutUser.java new file mode 100644 index 00000000000..4f66e83c80e --- /dev/null +++ b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLogoutUser.java @@ -0,0 +1,62 @@ +/* +* 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. +*/ + +package samples; + +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()) { + + // Remove the session from Redis + Long totalRemoved = jedis.del(TOKEN); + + // When no session is found to remove, + // that means the user is not logged in + if (totalRemoved == 0) { + System.out.printf("User %s is not logged in.%n", TOKEN); + return; + } + + System.out.printf("User %s has been logged out.%n", TOKEN); + } catch (Exception e) { + System.err.printf("Error connecting to Valkey: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreRemoveItemFromBasket.java b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreRemoveItemFromBasket.java new file mode 100644 index 00000000000..4128d667e0e --- /dev/null +++ b/memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreRemoveItemFromBasket.java @@ -0,0 +1,80 @@ +/* +* 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. +*/ + +package samples; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +public final class MemorystoreRemoveItemFromBasket { + + /** 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 removed from the user's basket */ + private static final String ITEM_ID = "ITEM_ID"; + + /** Quantity of items to be removed */ + private static final Integer REMOVE_QUANTITY = -1; + + private MemorystoreRemoveItemFromBasket() { + // No-op; won't be called + } + + /** + * Removes an item from a user's basket in 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()) { + + String basketKey = "basket:" + USER_ID; + + // Remove the item from the user's basket + long newQty = jedis.hincrBy(basketKey, ITEM_ID, REMOVE_QUANTITY); + + // Print the item removed + System.out.printf("Removed %d items from basket: %s%n", REMOVE_QUANTITY, ITEM_ID); + + // Remove the item if the quanitity is less than or equal to 0 + if (newQty <= 0) { + // Remove the item from the basket + Long totalRemoved = jedis.hdel(basketKey, ITEM_ID); + + // When no item is found to remove, + // that means the item is not in the basket + if (totalRemoved == 0) { + System.out.printf("Item %s not found in basket: %s%n", ITEM_ID, USER_ID); + return; + } + + // print the item removed + System.out.printf("Removed item from basket: %s%n", ITEM_ID); + } + } catch (Exception e) { + System.err.printf("Error connecting to Valkey: %s%n", e.getMessage()); + } + } +}