Reputation: 301
I want to cache some data across PHP requests rather than pull it from the database during each call. The data applies universally regardless of user/session. What is the lightest method of achieving this? I cannot guarantee write access to the drive or require APC. Is there a way to acheive this with vanilla PHP? Thanks!
Upvotes: 9
Views: 4648
Reputation: 61773
If you want your website to perform well you should always try and install APC. APC is primarily/also an opcode-cache which speeds up your website tremendously, but below I still try and give you some fast alternatives.
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.
redistogo.com offers free redis server(FAST) with 5MB of memory. You could next just use a plain vanilla redis-client like for example predis to connect to the redis-server and cache your data.
Is there a way to acheive this with vanilla PHP? Thanks!
You could try to use Cache_Lite(Vanilla library) and try to write to /dev/shm(Shared memory), but you need linux os for that. The introduction page shows the basic. If possible you should use cache_dir => '/dev/shm'
// Set a few options
$options = array(
'cacheDir' => '/dev/shm',
'lifeTime' => 3600
);
You could always/also use this package to write to disc(SLOW compared to memory), but is also caching and could speed up your website.
Upvotes: 3
Reputation: 6431
To have a typical system/application cache, you either need file system write access or have something like APC available.
The only other way to cache data is to set it to the session for each user, where if the session key "my-cache" doesn't exist, get from the database and set to the session. Just like with any other caching method you'll also have to think about unsetting the cache when data gets updated etc.
It's not ideal at all to have application data cached on the client side, but if you need cache where caching options aren't available, then it's your only option.
Upvotes: 0
Reputation: 1615
There are 3 'ways' that come to my mind when you ask this, but before I sum them up let me say that PHP is not designed to do what you want. There is no 'universal' caching system for queried (or accumulated) data. However, you can do several things to speed up your page loads and data requests:
The first sessions is rather obvious, but you should only use it if the data is user dependent (if not try to avoid it). For caching file you will need write access, but this is the best method for site wide data. Just use either serialized arrays (plain file) or XML data, whatever suits your needs. The last one is a great way to speed up MySQL requests (if they are the same every time and you don't have any write access to the system). You cah cache the query in the database, by using cached select.
As you can see all these methods have their own advantages and disadvantages, but you can use them all however you like. One side-note however is that it takes time to load cached data from file or session, so you should check whether caching actually speeds up requests!
Upvotes: 15
Reputation: 9121
There's many ways of 'caching', a database can be one of them. Another option you can try is XML files, but it all depends on your specific problem.
Upvotes: 3
Reputation: 2293
I don't think it's possible. You can still preload the data just once into a PHP session which will definitely reduce the number of database queries.
Upvotes: 0