public Task EmptyCartAsync(string userId) { Console.WriteLine($"EmptyCartAsync called with userId={userId}"); userCartItems[userId] = new Hipstershop.Cart(); return(Task.CompletedTask); }
public async Task AddItemAsync(string userId, string productId, int quantity) { Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}"); EnsureRedisConnected(); var db = redis.GetDatabase(); var value = await db.HashGetAsync(userId, CART_FIELD_NAME); Hipstershop.Cart cart; if (value.IsNull) { cart = new Hipstershop.Cart(); cart.UserId = userId; cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } else { cart = JsonSerializer.Deserialize <Hipstershop.Cart>(value); var existingItem = cart.Items.SingleOrDefault(i => i.ProductId == productId); if (existingItem == null) { cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } else { existingItem.Quantity += quantity; } } await db.HashSetAsync(userId, new[] { new HashEntry(CART_FIELD_NAME, JsonSerializer.Serialize(cart)) }); }
public Task AddItemAsync(string userId, string productId, int quantity) { Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}"); var newCart = new Hipstershop.Cart { UserId = userId, Items = { new Hipstershop.CartItem { ProductId = productId, Quantity = quantity } } }; userCartItems.AddOrUpdate(userId, newCart, (k, exVal) => { // If the item exists, we update its quantity var existingItem = exVal.Items.SingleOrDefault(item => item.ProductId == productId); if (existingItem != null) { existingItem.Quantity += quantity; } else { exVal.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } return(exVal); }); return(Task.CompletedTask); }
private static byte[] ToByteArray(Hipstershop.Cart cart) { if (cart == null) { return(null); } return(cart.ToByteArray()); }
public Task <Hipstershop.Cart> GetCartAsync(string userId) { Console.WriteLine($"GetCartAsync called with userId={userId}"); Hipstershop.Cart cart = null; if (!userCartItems.TryGetValue(userId, out cart)) { Console.WriteLine($"No carts for user {userId}"); return(Task.FromResult(emptyCart)); } return(Task.FromResult(cart)); }
public async Task EmptyCartAsync(string userId) { Console.WriteLine($"EmptyCartAsync called with userId={userId}"); try { var cart = new Hipstershop.Cart(); await _cache.SetAsync(userId, cart.ToByteArray()); } catch (Exception ex) { throw new RpcException(new Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}")); } }
public RedisCartStore(string redisAddress) { // Serialize empty cart into byte array. var cart = new Hipstershop.Cart(); emptyCartBytes = cart.ToByteArray(); connectionString = $"{redisAddress},ssl=false,allowAdmin=true,connectRetry=5"; redisConnectionOptions = ConfigurationOptions.Parse(connectionString); // Try to reconnect if first retry failed (up to 5 times with exponential backoff) redisConnectionOptions.ConnectRetry = REDIS_RETRY_NUM; redisConnectionOptions.ReconnectRetryPolicy = new ExponentialRetry(100); redisConnectionOptions.KeepAlive = 180; }
public async Task AddItemAsync(string userId, string productId, int quantity) { Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}"); try { EnsureRedisConnected(); var db = redis.GetDatabase(); // Access the cart from the cache var value = await db.HashGetAsync(userId, CART_FIELD_NAME); Hipstershop.Cart cart; if (value.IsNull) { cart = new Hipstershop.Cart(); cart.UserId = userId; cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } else { cart = Hipstershop.Cart.Parser.ParseFrom(value); var existingItem = cart.Items.SingleOrDefault(i => i.ProductId == productId); if (existingItem == null) { cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } else { existingItem.Quantity += quantity; } } await db.HashSetAsync(userId, new[] { new HashEntry(CART_FIELD_NAME, ToByteArray(cart)) }); } catch (Exception ex) { throw new RpcException(new Grpc.Core.Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}")); } }
public async Task AddItemAsync(string userId, string productId, int quantity) { Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}"); try { Hipstershop.Cart cart; var value = await _cache.GetAsync(userId); if (value == null) { cart = new Hipstershop.Cart(); cart.UserId = userId; cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } else { cart = Hipstershop.Cart.Parser.ParseFrom(value); var existingItem = cart.Items.SingleOrDefault(i => i.ProductId == productId); if (existingItem == null) { cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity }); } else { existingItem.Quantity += quantity; } } await _cache.SetAsync(userId, cart.ToByteArray()); } catch (Exception ex) { throw new RpcException(new Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}")); } }