R
R
Roman Rakzin2015-02-21 21:10:33
PHP
Roman Rakzin, 2015-02-21 21:10:33

How to handle certain memcache values?

Good afternoon.
I cache with the keys people_1111, people_1112, people_1113...
and also data_1111, data_1112, data_1113...
I need to select values ​​only relative to people_ and perform calculations with each of them or get a key/value array.
How to implement it?
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ezh, 2015-04-14
@ezh

<?php
class People implements IteratorAggregate {
    private $c;
    public function __construct($host, $port) {
        // TODO handle errors
        $this->c = memcache_connect($host, $port);
    }
    public function add($id, $value) {
        memcache_set($this->c, "people_$id", $value);
        // TODO race condition
        $list = memcache_get($this->c, "peoples");
        $list[$id] = true;
        memcache_set($this->c, "peoples", $list);
    }
    public function getIterator() {
        $list = memcache_get($this->c, "peoples");
        foreach ($list as $item => $_) yield memcache_get($this->c, "people_$item");
    }
}
$People = new People('localhost', 11211);
$People->add(1112, "first");
$People->add(1115, shell_exec("uname -v"));
$People->add(2000, microtime(true));
foreach ($People as $item) {
    echo $item, "\n";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question