Best way to setup a variable for Global use in Cod

2019-02-19 09:10发布

问题:

I'm using CodeIgniter and I have user data that needs to be stored in some way then updated. Basic things like name, id, points, comment count etc

At the moment when the user logs in I simply get their data and store it in a session. Now obviously the name and id would never change, but the Points and Comment count etc will change from page to page depending on the user actions.

As this data will be constantly changing, what would be the best way to store it , update it and make it available throughout my app? I'm not sure if updating the session every time the user performs an action is the best way though?

I thought about maybe using a CodeIgniter hook to run a function that sets up a variable ready for use?

Whats the best way? Any ideas?

Thanks, Sean Jenkins

回答1:

Why can't you store the data in a database? If your worried about performance you could use something like Memcache or APC to cache it but it seems like comment count and points are data that need to persist between sessions.

You would still need to write the data to the database as well as the cache but then you could read the data directly from the cache instead of querying for it.