Stefan Cebulak
Stefan Cebulak

Reputation: 179

MVC - How to cache frequently used and changed User_Data (for web game)

I'm trying to develop a web game. In that game i have a toolbar, which is shown on almost all pages. Toolbar contains user info such as: - coins (cash) - level - name - group name - experience points - skills - notifications

and more...

Some of them are changing rarely, but some (like experience, skills, notifications or coins) can change quite often.

So, i don't want to query database each time someone reloads a page. I'm thinking of caching this data in memory of server (updating it each time something changes).

Is there any known, good approach? Or should i forget and make extra 2-3 SQL queries on each page load?

Upvotes: 0

Views: 148

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

Is this a single user game or a multi-user game? If it's a multi-user game, then you're going to have to have some kind of player state that's held for all your currently active players in the game, that's a perfect place for this information. If it's a single user game, then you probably want to keep as little state on the server as possible so you're more likely to load it from disk, or using a distributed caching system.

The problem here is that we can't really tell you what to do because all of it depends on way too many factors... you're just going to have to figure this out as you go along.

My suggestion though, don't pre-maturely think you need to optimize something. Just do it however you think you can, and if you need better performance, then worry about that later.

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239290

"Frequently changed" pretty much excludes caching. I think there's an obsession with optimization that makes people want to cache everything, but you have a database for a reason, and it's perfectly acceptable to query it on occasion.

What you can do, though, is cache portions. Something like Level will typically not change that often as it takes a while to go from one to the next. That could be cached, and it's easy enough to just bust the cache when the level changes so that it's will be forced to be reloaded then. Other things like Coins a user should always have an up-to-date and accurate count. You'll have to make these determinations, obviously.

Long and short, though, if the user should always have an accurate count of display of the thing, then you simply can't cache it. Just go ahead and do the queries you need to do and move on.

Upvotes: 2

Related Questions