added redis integration and command line arguments handling to cart service. Didn't test in the cloud yet
This commit is contained in:
parent
572c02f588
commit
8860d8bfdd
7 changed files with 184 additions and 0 deletions
41
src/cartservice/cartstore/LocalCartStore.cs
Normal file
41
src/cartservice/cartstore/LocalCartStore.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System.Collections.Concurrent;
|
||||
using cartservice.interfaces;
|
||||
|
||||
namespace cartservice.cartstore
|
||||
{
|
||||
internal class LocalCartStore
|
||||
{
|
||||
// Maps between user and their cart
|
||||
private ConcurrentDictionary<string, Cart> userCartItems = new ConcurrentDictionary<string, Cart>();
|
||||
|
||||
public void AddItem(string userId, string productId, int quantity)
|
||||
{
|
||||
Cart cart;
|
||||
if (!userCartItems.TryGetValue(userId, out cart))
|
||||
{
|
||||
cart = new Cart(userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
cart = userCartItems[userId];
|
||||
}
|
||||
cart.AddItem(productId, quantity);
|
||||
}
|
||||
|
||||
public void EmptyCart(string userId)
|
||||
{
|
||||
Cart cart;
|
||||
if (userCartItems.TryGetValue(userId, out cart))
|
||||
{
|
||||
cart.EmptyCart();
|
||||
}
|
||||
}
|
||||
|
||||
public Cart GetCart(string userId)
|
||||
{
|
||||
Cart cart = null;
|
||||
userCartItems.TryGetValue(userId, out cart);
|
||||
return cart;
|
||||
}
|
||||
}
|
||||
}
|
74
src/cartservice/cartstore/RedisCartStore.cs
Normal file
74
src/cartservice/cartstore/RedisCartStore.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using cartservice.interfaces;
|
||||
using Google.Protobuf;
|
||||
using Hipstershop;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace cartservice.cartstore
|
||||
{
|
||||
public class RedisCartStore : ICartStore
|
||||
{
|
||||
private const string CART_FIELD_NAME = "cart";
|
||||
|
||||
private readonly ConnectionMultiplexer redis;
|
||||
|
||||
private readonly byte[] emptyCartBytes;
|
||||
|
||||
public RedisCartStore(string redisAddress)
|
||||
{
|
||||
// Serialize empty cart into byte array.
|
||||
var cart = new Hipstershop.Cart();
|
||||
emptyCartBytes = cart.ToByteArray();
|
||||
|
||||
string connectionString = $"{redisAddress},ssl=false,allowAdmin=true";
|
||||
Console.WriteLine("Connecting to Redis: " + connectionString);
|
||||
redis = ConnectionMultiplexer.Connect(connectionString);
|
||||
}
|
||||
|
||||
public async Task AddItemAsync(string userId, string productId, int quantity)
|
||||
{
|
||||
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();
|
||||
}
|
||||
else
|
||||
{
|
||||
cart = Hipstershop.Cart.Parser.ParseFrom(value);
|
||||
}
|
||||
|
||||
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
|
||||
}
|
||||
|
||||
public async Task EmptyCartAsync(string userId)
|
||||
{
|
||||
var db = redis.GetDatabase();
|
||||
|
||||
// Update the cache with empty cart for given user
|
||||
await db.HashSetAsync(userId, new[] { new HashEntry(CART_FIELD_NAME, emptyCartBytes) });
|
||||
}
|
||||
|
||||
public async Task<Hipstershop.Cart> GetCartAsync(string userId)
|
||||
{
|
||||
var db = redis.GetDatabase();
|
||||
|
||||
// Access the cart from the cache
|
||||
var value = await db.HashGetAsync(userId, CART_FIELD_NAME);
|
||||
|
||||
Hipstershop.Cart cart = null;
|
||||
if (!value.IsNull)
|
||||
{
|
||||
cart = Hipstershop.Cart.Parser.ParseFrom(value);
|
||||
}
|
||||
|
||||
return cart;
|
||||
}
|
||||
}
|
||||
}
|
12
src/cartservice/interfaces/ICartStore.cs
Normal file
12
src/cartservice/interfaces/ICartStore.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace cartservice.interfaces
|
||||
{
|
||||
internal interface ICartStore
|
||||
{
|
||||
Task AddItemAsync(string userId, string productId, int quantity);
|
||||
Task EmptyCartAsync(string userId);
|
||||
|
||||
Task<Hipstershop.Cart> GetCartAsync(string userId);
|
||||
}
|
||||
}
|
11
src/cartservice/run_redis_emulator_windows.bat
Normal file
11
src/cartservice/run_redis_emulator_windows.bat
Normal file
|
@ -0,0 +1,11 @@
|
|||
@echo off
|
||||
rem install redis on windows using choco
|
||||
rem choco install redis-64
|
||||
|
||||
rem run redis
|
||||
redis-server --daemonize yes
|
||||
|
||||
rem testing locally
|
||||
rem redis-cli
|
||||
rem SET foo bar
|
||||
rem GET foo
|
Loading…
Add table
Add a link
Reference in a new issue