简体   繁体   English

更新数据库和缓存中数据的最佳实践

[英]Best practice updating data in DB and in cache

I have a game written in C#.我有一个用 C# 编写的游戏。 I have two APIs.我有两个 API。 The first updates a record in the DB and sends the response and the second gets the record.第一个更新数据库中的记录并发送响应,第二个获取记录。

I am using Memcached to get it from the cache instead of from the database since the callers of the API are a lot and I don't want to overload the number of calls to the database.我使用Memcached从缓存而不是从数据库中获取它,因为 API 的调用者很多,我不想使对数据库的调用次数过载。 What I am doing is I am updating the data in the database and manually updating the value of the key in the cache.我正在做的是我正在更新数据库中的数据并手动更新缓存中键的值。 I am using this approach and not the one where you clear the key from the cache and next time you get it from the DB and save it back in the cache.我正在使用这种方法,而不是从缓存中清除密钥,下次从数据库中获取它并将其保存回缓存中的方法。

Now, my question is, Is my code correct?现在,我的问题是,我的代码正确吗? Is it good practice?这是好的做法吗? And what if the record is saved in the database and not in the cache?如果记录保存在数据库中而不是缓存中呢? How to prevent that from happening?如何防止这种情况发生?

private async Task UpdateStateAsync(State State)
{
      await _unitOfWork.State
           .UpdateAsync(ts => ts.Id == State.Id, ts => new State()
           {
                Status = State.Status
           });
}

private async Task UpdateStateCacheItemAsync(State state)
{
     await _cache.SaveAsync($"State_{state.GameId}", state); //override data if same key exists
}

This is how I call them in my API body这就是我在 API 主体中调用它们的方式

//some logic here
await UpdateStateAsync(state);  //update the record in DB
await _unitOFWork.Complete();   //commit changes to DB
await UpdateStateCacheItemAsync(state);   //update item in cache

The idea is good.这个主意不错。 That is exactly how we also would want cache to be implemented.这正是我们希望缓存实现的方式。 Though the logic has a flaw.虽然逻辑有问题。 You know that Memcache is a form of cache, which stores its objects in the memory.你知道 Memcache 是一种缓存形式,它把它的对象存储在内存中。 Memcache itself may decide to evict any object if it is not used for a long time.如果长时间不使用,Memcache 本身可能会决定驱逐任何对象。 Or the server may fail and all objects in Memcache may get deleted.或者服务器可能会失败并且 Memcache 中的所有对象都可能被删除。

So your logic to write is correct.所以你写的逻辑是正确的。 Write/update to database and then also update to cache.写入/更新到数据库,然后也更新到缓存。

To read though, I would read from Memcache, if I do not get what I want, my fallback would be to read from the database.不过要读取,我会从 Memcache 读取,如果我没有得到我想要的东西,我的后备方法是从数据库中读取。

To improve further while writing updates to Memcache, I would only write it if it is already present in Memcache.为了在向 Memcache 写入更新时进一步改进,我只会在它已经存在于 Memcache 中时才写入它。 The assumption being that if it is not already present it may not be required and Memcache may evict that object anyways.假设是,如果它尚未存在,则可能不需要它,而且 Memcache 可能会驱逐该对象。 Plus in adding unnecessary objects you may evict necessary objects.另外,在添加不必要的对象时,您可能会驱逐必要的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM