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!
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With