Memcached + PHP storing data and fetching + Unique IP counter + Referrer
i want make an unique counter which tracks following, every referrer and ip from开发者_JAVA技巧 where the user comes. Memcached should store that data and than it should be entered into the database after 5 mins or so. And also it should check for duplicate entries that not twice the ip is written to the DB
I have an idea to do it without memcache, but than every sec something would be written into the database which would make the site slow.
I can cache basic SQL with memcache but i have no clue how make that above, just learned coding. So im a totally noob :)
Thank you for your help.
As I understand it - you are using a RDBMS to store the information for long term. I can suggest 2 things:
1. Store information in key/value store such as Memcached and write it to RDBMS periodically through a background process (CRON job). With this solution you would do something like this in your application code:
$m = new Memcached();
$m->addServer('localhost', 11211);
$m->setOption(Memcached::OPT_COMPRESSION, false);
$m->append('some_unique_id', '|' . time() . ':' . base64_encode($_SERVER['HTTP_REFERER']));
And then you would have a CRON job doing something like this:
$m = new Memcached();
$m->addServer('localhost', 11211);
foreach($list_of_unique_ids as $uid)
{
$raw_data = $m->get($uid);
$m->delete($uid);
$uid_safe = mysql_real_escape_string($uid);
$items = explode('|', $raw_data);
$sql = array();
foreach($items as $rec)
{
if (!empty($rec))
{
list($timestamp, $ref_base64) = explode(':', $rec, 2);
$ref = base64_decode($ref_base64);
$ref_safe = mysql_real_escape_string($ref);
$sql[] = "('" . $uid_safe . "', " . (int) $timestamp . ", '" . $ref_safe . "')";
}
}
mysql_query("INSERT INTO some_table (user_id, ts, ref) VALUES " . implode(',', $sql));
}
2. Use a more advanced key/value store such as Redis or MongoDB as primary means of long-term storage. Just pick a database engine with huge write throughput and call it a day.
> i want make an unique counter which
> tracks following, every referrer and
> ip from where the user comes.
> Memcached should store that data and
> than it should be entered into the
> database after 5 mins or so. And also
> it should check for duplicate entries
> that not twice the ip is written to
> the DB
I would advise you to use redis to implement this because redis has all the commands needed to do this efficiently and has persistent snapshots. To count you simply use the incrby/incr => decr/decbry commands.
If you installed memcached on your box, then installing redis is going to be a sinch. Just make
will be enough. A popular client to connect to redis from PHP is predis.
If you can not install software you also have the option to use the free plan(5 MB memory, 1 Database, but no backups) from http://redistogo.com. Than you need to do the backups to MySQL manually because snapshots are disabled with free plan(probably better of getting a dedicated box or buying mini plan for $5/month).
精彩评论