Design pattern for memcached data caching

前端 未结 4 702
盖世英雄少女心
盖世英雄少女心 2021-02-03 11:51

It\'s easy to wrap optional memcached caching around your existing database queries. For example:

Old (DB-only):

function getX
    x = get from db
    r         


        
4条回答
  •  孤街浪徒
    2021-02-03 12:02

    Well, I guess that's something you'll have to live with. Memcahced will work the best if you don't really do stuff in batches. For example it's great for stuff like "where are the things for this user? Here is a bunch of things for this user." This doesn't really mean that this query doesn't do batches. Of course it will - if some of the user stuff is stuff like his/her posts.

    I guess the problem you'll have is cases where you are mixing queries that need to get an item from the DB on its own and some that get bunch of the same kind of the previous items.

    There is always a flip side to the situation. If you really want to get hairy with your implementation you can change your batch queries to not include the items already present in memcached.Very very ugly...

    In my opinion it always comes down to "which queries do I really want to cache?"

    EDIT:

    The way I would go about this is:

    • Single-item query - if in memcached, use that one, otherwise fetch from DB and update memcached.
    • Batch query - don't worry about which items are in memcached, just get everything and update memcached.

    This of course assumes that the batch queries already take hell a lot more time to complete and so it I'm already spending so much time I can live with external lookups to already cached items.

    However, eventually, your cache will contain a lot of the items if you use the batch queries a lot. Therefore you'll have to strike the balance for determining at which point you still want to perform the database lookups. Good thing is if the batch query is earlier in the life cycle of your applications, then everything will be cached earlier. After the first batch query you can tell yourself that you don't need to fetch from DB anymore unless the data in the cache is invalidated by updates or deletes.

提交回复
热议问题