Skip to content

Commit a3d4d0a

Browse files
committed
docs(memorystore): added valkey session snippets
1 parent 4f6d504 commit a3d4d0a

File tree

6 files changed

+435
-0
lines changed

6 files changed

+435
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2020 Google LLC
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>com.example.memorystore</groupId>
24+
<artifactId>caching</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
27+
<name>Google Cloud Memorystore Sample</name>
28+
<url>http://www.example.com</url>
29+
30+
<properties>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
<maven.compiler.release>11</maven.compiler.release>
33+
</properties>
34+
35+
<dependencyManagement>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.junit</groupId>
39+
<artifactId>junit-bom</artifactId>
40+
<version>5.11.0</version>
41+
<type>pom</type>
42+
<scope>import</scope>
43+
</dependency>
44+
</dependencies>
45+
</dependencyManagement>
46+
47+
<dependencies>
48+
<!-- Jedis Redis client library -->
49+
<dependency>
50+
<groupId>redis.clients</groupId>
51+
<artifactId>jedis</artifactId>
52+
<version>4.3.1</version>
53+
</dependency>
54+
55+
<!-- Google Cloud Client Libraries -->
56+
<dependency>
57+
<groupId>com.google.cloud</groupId>
58+
<artifactId>google-cloud-core</artifactId>
59+
<version>2.16.0</version>
60+
</dependency>
61+
62+
<!-- Unit testing libraries -->
63+
<dependency>
64+
<groupId>org.junit.jupiter</groupId>
65+
<artifactId>junit-jupiter-api</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.junit.jupiter</groupId>
70+
<artifactId>junit-jupiter-params</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<pluginManagement>
77+
<plugins>
78+
<plugin>
79+
<artifactId>maven-clean-plugin</artifactId>
80+
<version>3.4.0</version>
81+
</plugin>
82+
<plugin>
83+
<artifactId>maven-resources-plugin</artifactId>
84+
<version>3.3.1</version>
85+
</plugin>
86+
<plugin>
87+
<artifactId>maven-compiler-plugin</artifactId>
88+
<version>3.13.0</version>
89+
</plugin>
90+
<plugin>
91+
<artifactId>maven-surefire-plugin</artifactId>
92+
<version>3.3.0</version>
93+
</plugin>
94+
<plugin>
95+
<artifactId>maven-jar-plugin</artifactId>
96+
<version>3.4.2</version>
97+
</plugin>
98+
<plugin>
99+
<artifactId>maven-install-plugin</artifactId>
100+
<version>3.1.2</version>
101+
</plugin>
102+
<plugin>
103+
<artifactId>maven-deploy-plugin</artifactId>
104+
<version>3.1.2</version>
105+
</plugin>
106+
<plugin>
107+
<artifactId>maven-site-plugin</artifactId>
108+
<version>3.12.1</version>
109+
</plugin>
110+
<plugin>
111+
<artifactId>maven-project-info-reports-plugin</artifactId>
112+
<version>3.6.1</version>
113+
</plugin>
114+
</plugins>
115+
</pluginManagement>
116+
</build>
117+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR IMPLIED WARRANTIES OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import redis.clients.jedis.Jedis;
18+
import redis.clients.jedis.JedisPool;
19+
20+
public final class MemorystoreAddItemToBasket {
21+
22+
/** Replace the Memorystore instance ID. */
23+
private static final String INSTANCE_ID = "INSTANCE_ID";
24+
25+
/** Replace the Memorystore port, if not the default port. */
26+
private static final int PORT = 6379;
27+
28+
/** User ID for managing the basket. */
29+
private static final String USER_ID = "USER_ID";
30+
31+
/** Item to be added to the user's basket. */
32+
private static final String ITEM_ID = "ITEM_ID";
33+
34+
/** Set how many items to be added to the basket. */
35+
private static final int ITEM_COUNT = 1;
36+
37+
private MemorystoreAddItemToBasket() {
38+
// No-op constructor to prevent instantiation
39+
}
40+
41+
/**
42+
* Adds an item to a user's basket in Memorystore.
43+
*
44+
* @param args command-line arguments
45+
*/
46+
public static void main(final String[] args) {
47+
// Create a Jedis connection pool
48+
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
49+
Jedis jedis = pool.getResource()) {
50+
51+
String basketKey = "basket:" + USER_ID;
52+
53+
// Add items to the user's basket
54+
jedis.hincrBy(basketKey, ITEM_ID, ITEM_COUNT);
55+
System.out.printf("Added %d items to basket: %s%n", ITEM_COUNT, ITEM_ID);
56+
57+
// Verify the item is in the basket
58+
boolean exists = jedis.hexists(basketKey, ITEM_ID);
59+
if (exists) {
60+
System.out.printf("Item successfully added: %s%n", ITEM_ID);
61+
} else {
62+
System.out.printf("Failed to add item: %s%n", ITEM_ID);
63+
}
64+
} catch (Exception e) {
65+
System.err.printf("Error connecting to Redis: %s%n", e.getMessage());
66+
}
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR IMPLIED WARRANTIES OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import redis.clients.jedis.Jedis;
18+
import redis.clients.jedis.JedisPool;
19+
20+
public final class MemorystoreClearBasket {
21+
22+
/** Replace the Memorystore instance ID. */
23+
private static final String INSTANCE_ID = "INSTANCE_ID";
24+
25+
/** Replace the Memorystore port, if not the default port. */
26+
private static final int PORT = 6379;
27+
28+
/** User ID for managing the basket */
29+
private static final String USER_ID = "USER_ID";
30+
31+
private MemorystoreClearBasket() {
32+
// No-op; won't be called
33+
}
34+
35+
/**
36+
* Clears a user's basket in Memorystore.
37+
*
38+
* @param args command-line arguments
39+
*/
40+
public static void main(final String[] args) {
41+
// Connect to the Memorystore instance
42+
JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
43+
44+
try (Jedis jedis = pool.getResource()) {
45+
String basketKey = "basket:" + USER_ID;
46+
47+
// Delete the basket (remove all items)
48+
long deleted = jedis.del(basketKey);
49+
50+
// Verify if the basket was cleared
51+
if (deleted > 0) {
52+
System.out.printf("Basket cleared successfully for user: %s%n", USER_ID);
53+
} else {
54+
System.out.printf("No basket found for user: %s%n", USER_ID);
55+
}
56+
} catch (Exception e) {
57+
System.err.printf("Error connecting to Redis: %s%n", e.getMessage());
58+
}
59+
}
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR IMPLIED WARRANTIES OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import redis.clients.jedis.Jedis;
18+
import redis.clients.jedis.JedisPool;
19+
20+
import java.util.UUID;
21+
22+
public final class MemorystoreLoginUser {
23+
24+
/** Replace the Memorystore instance id. */
25+
private static final String INSTANCE_ID = "INSTANCE_ID";
26+
27+
/** Replace the Memorystore port, if not the default port. */
28+
private static final int PORT = 6379;
29+
30+
/** User ID for login */
31+
private static final String USER_ID = "USER_ID";
32+
33+
/** Session expiration time in seconds (30 minutes) */
34+
private static final int SESSION_TIMEOUT = 1800;
35+
36+
private MemorystoreLoginUser() {
37+
// No-op; won't be called
38+
}
39+
40+
/**
41+
* Logs in a user by creating a session in Memorystore.
42+
*
43+
* @param args command-line arguments
44+
*/
45+
public static void main(final String[] args) {
46+
// Connect to the Memorystore instance
47+
JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
48+
49+
try (Jedis jedis = pool.getResource()) {
50+
51+
// Generate a session token
52+
String sessionToken = UUID.randomUUID().toString();
53+
54+
// Store the session token in Redis with an expiration time
55+
jedis.setex(sessionToken, SESSION_TIMEOUT, USER_ID);
56+
System.out.printf("User %s logged in with session: %s%n", USER_ID, sessionToken);
57+
} catch (Exception e) {
58+
System.err.printf("Error connecting to Redis: %s%n", e.getMessage());
59+
}
60+
}
61+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR IMPLIED WARRANTIES OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import redis.clients.jedis.Jedis;
18+
import redis.clients.jedis.JedisPool;
19+
20+
public final class MemorystoreLogoutUser {
21+
22+
/** Replace the Memorystore instance id. */
23+
private static final String INSTANCE_ID = "INSTANCE_ID";
24+
25+
/** Replace the Memorystore port, if not the default port. */
26+
private static final int PORT = 6379;
27+
28+
/** User ID token for logout */
29+
private static final String TOKEN = "TOKEN";
30+
31+
private MemorystoreLogoutUser() {
32+
// No-op; won't be called
33+
}
34+
35+
/**
36+
* Logs out a user by deleting their session from Memorystore.
37+
*
38+
* @param args command-line arguments
39+
*/
40+
public static void main(final String[] args) {
41+
// Connect to the Memorystore instance
42+
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
43+
Jedis jedis = pool.getResource()) {
44+
45+
// Check if the session exists
46+
if (!jedis.exists(TOKEN)) {
47+
System.out.printf("User %s is not logged in.%n", TOKEN);
48+
return;
49+
}
50+
51+
// Remove the session from Redis
52+
jedis.del(TOKEN);
53+
System.out.printf("User %s has been logged out.%n", TOKEN);
54+
} catch (Exception e) {
55+
System.err.printf("Error connecting to Redis: %s%n", e.getMessage());
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)