Skip to content

session snippets adjustments based on feedback #21

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ private MemorystoreClearBasket() {
*/
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;
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
Jedis jedis = pool.getResource()) {

// Delete the basket (remove all items)
long deleted = jedis.del(basketKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ private MemorystoreLoginUser() {
*/
public static void main(final String[] args) {
// Connect to the Memorystore instance
JedisPool pool = new JedisPool(INSTANCE_ID, PORT);

try (Jedis jedis = pool.getResource()) {
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
Jedis jedis = pool.getResource()) {

// Generate a session token
String sessionToken = UUID.randomUUID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ public static void main(final String[] args) {
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
Jedis jedis = pool.getResource()) {

// Check if the session exists
if (!jedis.exists(TOKEN)) {
// 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;
}

// 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ private MemorystoreRemoveItemFromBasket() {
*/
public static void main(final String[] args) {
// Connect to the Memorystore instance
JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
try (JedisPool pool = new JedisPool(INSTANCE_ID, PORT);
Jedis jedis = pool.getResource()) {

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

// Remove the item from the user's basket
Expand All @@ -59,7 +59,14 @@ public static void main(final String[] args) {
// Remove the item if the quanitity is less than or equal to 0
if (newQty <= 0) {
// Remove the item from the basket
jedis.hdel(basketKey, ITEM_ID);
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);
Expand Down