Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Natively persisting data across PHP requests

Tags:

php

caching

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!

like image 635
Derg Avatar asked Jun 25 '11 17:06

Derg


3 Answers

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:

  1. Session (Requirements: Cookie support for a user and requeries for every new user)
  2. Cache File (Requirements: Write Access to the drive)
  3. Caching the Query (Requirements: Able to cache the query in MySQL)

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!

like image 149
KilZone Avatar answered Nov 10 '22 11:11

KilZone


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.

like image 38
Kokos Avatar answered Nov 10 '22 11:11

Kokos


P.S:

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.

redistogo.com

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.

/dev/shm

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.

like image 3
Alfred Avatar answered Nov 10 '22 12:11

Alfred