Exemplo n.º 1
0
        public async Task UpdateAsync(CachedCar car)
        {
            Car c = new Car
            {
                Id     = car.Id,
                Colour = car.Colour,
                CountryManufactured = car.CountryManufactured,
                Make  = car.Make,
                Model = car.Model,
                Price = car.Price,
                Year  = car.Year
            };

            await mainStoring.Update(c);

            CachedCar Cc = mainCachedStoring.AddOrUpdate(car.Id, car, (key, existingCar) =>
            {
                existingCar.Make  = car.Make;
                existingCar.Model = car.Model;
                existingCar.Year  = car.Year;
                existingCar.CountryManufactured = car.CountryManufactured;
                existingCar.Colour = car.Colour;
                existingCar.Price  = car.Price;
                return(existingCar);
            });
        }
Exemplo n.º 2
0
        public async Task <int> AddAsync(CachedCar car)
        {
            Car c = new Car
            {
                Id     = car.Id,
                Colour = car.Colour,
                CountryManufactured = car.CountryManufactured,
                Make  = car.Make,
                Model = car.Model,
                Price = car.Price,
                Year  = car.Year
            };

            int repo_id = await mainStoring.Add(c);

            car.Id = repo_id;

            CachedCar Cc = mainCachedStoring.AddOrUpdate(repo_id, car, (key, existingCar) =>
            {
                existingCar.Make  = car.Make;
                existingCar.Model = car.Model;
                existingCar.Year  = car.Year;
                existingCar.CountryManufactured = car.CountryManufactured;
                existingCar.Colour = car.Colour;
                existingCar.Price  = car.Price;
                return(existingCar);
            });

            return(await Task.FromResult(repo_id));
        }
Exemplo n.º 3
0
 public async Task <ActionResult> Post([FromForm] CachedCar value)
 {
     try
     {
         value.Id = 0;
         return(Ok(await _cachedRepo.AddAsync(value)));
     }
     catch (InvalidOperationException ioe)
     {
         return(BadRequest(ioe.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex.Message));
     }
 }
Exemplo n.º 4
0
        public async Task <ActionResult> Put(int id, [FromForm] CachedCar value)
        {
            try
            {
                value.Id = id;
                await _cachedRepo.UpdateAsync(value);

                return(Ok());
            }
            catch (InvalidOperationException ioe)
            {
                return(StatusCode(404, ioe.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Exemplo n.º 5
0
        public CachedRepo()
        {
            List <Car> repo = mainStoring.GetAllCars().Result;

            foreach (Car c in repo)
            {
                CachedCar cc = new CachedCar(c);

                mainCachedStoring.AddOrUpdate(c.Id, cc, (key, existingCar) =>
                {
                    existingCar.Id    = c.Id;
                    existingCar.Make  = c.Make;
                    existingCar.Model = c.Model;
                    existingCar.Year  = c.Year;
                    existingCar.CountryManufactured = c.CountryManufactured;
                    existingCar.Colour = c.Colour;
                    existingCar.Price  = c.Price;
                    return(existingCar);
                });
            }
            CachedDateTime = DateTime.Now;
        }
Exemplo n.º 6
0
        private void SyncCacheWithRepo()
        {
            if (CachedDateTime < DateTime.Now.AddHours(-12))
            {
                if (Monitor.TryEnter(lck, 0))
                {
                    try
                    {
                        List <Car> repo = mainStoring.GetAllCars().Result; //Heavy
                        mainCachedStoring.Clear();

                        foreach (Car c in repo)
                        {
                            CachedCar cc = new CachedCar(c);

                            mainCachedStoring.AddOrUpdate(c.Id, cc, (key, existingCar) =>
                            {
                                existingCar.Id    = c.Id;
                                existingCar.Make  = c.Make;
                                existingCar.Model = c.Model;
                                existingCar.Year  = c.Year;
                                existingCar.CountryManufactured = c.CountryManufactured;
                                existingCar.Colour = c.Colour;
                                existingCar.Price  = c.Price;
                                return(existingCar);
                            });
                        }
                        CachedDateTime = DateTime.Now;
                    }
                    finally
                    {
                        Monitor.Exit(lck);
                    }
                }
            }
        }