Skip to content
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

Store cart in cookie instead of HttpSession #13

Closed
wants to merge 1 commit into from
Closed

Conversation

slandelle
Copy link
Member

Motivation:

Effort to reduce server memory usage

Motivation:

Effort to reduce server memory usage

@Controller
@RequestMapping("/cart")
@SuppressWarnings("unchecked")
public class CartController {

public static final class Carts {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should not be in Controller.

public static final class => should be a top class in its own file

@@ -1,19 +1,21 @@
package io.gatling.demostore;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First line of first changed file: only to have a closable thread.

I think we should change the session management instead of a workaround like that.
The cart being in the session is a normal usage of the session.
The session being stored in the server is not.
Either an accessible cache (ie memcache or redis) or using session from cookie (like Play does)

First quick search on the topic:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


} else {
HashMap<Integer, Cart> cart = (HashMap<Integer, Cart>) session.getAttribute("cart");
cart = Carts.fromCookieValue(cartCookie);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't fromCookieValue to manage the case that cartCookie is null?

Comment on lines 74 to +78
if (cart.containsKey(id)) {
int qty = cart.get(id).getQuantity();
cart.put(id, new Cart(id, product.getName(), product.getPrice(), ++qty, product.getImage()));
cart.put(id, new CartEntry(id, product.getName(), product.getPrice(), ++qty, product.getImage()));
} else {
cart.put(id, new Cart(id, product.getName(), product.getPrice(), 1, product.getImage()));
session.setAttribute("cart", cart);
cart.put(id, new CartEntry(id, product.getName(), product.getPrice(), 1, product.getImage()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (cart.containsKey(id)) {
int qty = cart.get(id).getQuantity();
cart.put(id, new Cart(id, product.getName(), product.getPrice(), ++qty, product.getImage()));
cart.put(id, new CartEntry(id, product.getName(), product.getPrice(), ++qty, product.getImage()));
} else {
cart.put(id, new Cart(id, product.getName(), product.getPrice(), 1, product.getImage()));
session.setAttribute("cart", cart);
cart.put(id, new CartEntry(id, product.getName(), product.getPrice(), 1, product.getImage()));
int qty = cart.containsKey(id) ? cart.get(id).getQuantity() : 0;
cart.put(id, new CartEntry(id, product.getName(), product.getPrice(), qty + 1, product.getImage()));

Comment on lines +203 to +214
private static void updateCartCookie(HttpServletResponse response, Map<Integer, CartEntry> cart) throws IOException {
Cookie cookie = new Cookie(Carts.COOKIE_NAME, Carts.toCookieValue(cart));
cookie.setPath("/");
response.addCookie(cookie);
}

private static void removeCartCookie(HttpServletResponse response) {
Cookie cookie = new Cookie(Carts.COOKIE_NAME, "");
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Carts inner class?


public static Map<Integer, CartEntry> fromCookieValue(String cookieValue) throws IOException {
if (cookieValue == null) {
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return null;
return new HashMap<>();


private static TypeReference<Map<Integer, CartEntry>> CART_TYPE = new TypeReference<Map<Integer, CartEntry>>() {};

public static Map<Integer, CartEntry> fromCookieValue(String cookieValue) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static Map<Integer, CartEntry> fromCookieValue(String cookieValue) throws IOException {
public static Map<Integer, CartEntry> fromCookieValue(String cookieValue) {

return null;
}

return JACKSON.readValue(Base64.getDecoder().decode(cookieValue), CART_TYPE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return JACKSON.readValue(Base64.getDecoder().decode(cookieValue), CART_TYPE);
try {
return JACKSON.readValue(Base64.getDecoder().decode(cookieValue), CART_TYPE);
} catch (IOException e) {
return new HashMap<>();
}

@slandelle slandelle closed this Jan 23, 2023
@slandelle slandelle deleted the stateless-cart branch January 23, 2023 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants