Skip to content

Commit 031a5c6

Browse files
CorieWcabljac
authored andcommitted
chore: make adjustments based on feedback
1 parent a3d4d0a commit 031a5c6

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Diff for: memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreClearBasket.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ private MemorystoreClearBasket() {
3939
*/
4040
public static void main(final String[] args) {
4141
// 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;
42+
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
43+
Jedis jedis = pool.getResource()) {
4644

4745
// Delete the basket (remove all items)
4846
long deleted = jedis.del(basketKey);

Diff for: memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLoginUser.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ private MemorystoreLoginUser() {
4444
*/
4545
public static void main(final String[] args) {
4646
// Connect to the Memorystore instance
47-
JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
48-
49-
try (Jedis jedis = pool.getResource()) {
47+
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
48+
Jedis jedis = pool.getResource()) {
5049

5150
// Generate a session token
5251
String sessionToken = UUID.randomUUID().toString();

Diff for: memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreLogoutUser.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ public static void main(final String[] args) {
4242
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
4343
Jedis jedis = pool.getResource()) {
4444

45-
// Check if the session exists
46-
if (!jedis.exists(TOKEN)) {
45+
// Remove the session from Redis
46+
Long totalRemoved = jedis.del(TOKEN);
47+
48+
// When no session is found to remove,
49+
// that means the user is not logged in
50+
if (totalRemoved == 0) {
4751
System.out.printf("User %s is not logged in.%n", TOKEN);
4852
return;
4953
}
5054

51-
// Remove the session from Redis
52-
jedis.del(TOKEN);
5355
System.out.printf("User %s has been logged out.%n", TOKEN);
5456
} catch (Exception e) {
5557
System.err.printf("Error connecting to Redis: %s%n", e.getMessage());

Diff for: memorystore/valkey/session/snippets/src/main/java/samples/MemorystoreRemoveItemFromBasket.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ private MemorystoreRemoveItemFromBasket() {
4545
*/
4646
public static void main(final String[] args) {
4747
// Connect to the Memorystore instance
48-
JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
48+
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
49+
Jedis jedis = pool.getResource()) {
4950

50-
try (Jedis jedis = pool.getResource()) {
5151
String basketKey = "basket:" + USER_ID;
5252

5353
// Remove the item from the user's basket
@@ -59,7 +59,14 @@ public static void main(final String[] args) {
5959
// Remove the item if the quanitity is less than or equal to 0
6060
if (newQty <= 0) {
6161
// Remove the item from the basket
62-
jedis.hdel(basketKey, ITEM_ID);
62+
Long totalRemoved = jedis.hdel(basketKey, ITEM_ID);
63+
64+
// When no item is found to remove,
65+
// that means the item is not in the basket
66+
if (totalRemoved == 0) {
67+
System.out.printf("Item %s not found in basket: %s%n", ITEM_ID, USER_ID);
68+
return;
69+
}
6370

6471
// print the item removed
6572
System.out.printf("Removed item from basket: %s%n", ITEM_ID);

0 commit comments

Comments
 (0)