How to choose a random value from storage using memcacheD in PHP (substitute for Redis RANDOMKEY)?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
7 views
Hello.


Model a situation (to change which, as usual, is not possible). Using memcached (Memcache is used by clientd in PHP) are stored, for example, quotes (text or even binary, but base64).


The keys are numbers of citations. Value — text citations. The numbers of the citations is inconsistent. That is, first to add quotes with numbers 100 and 101, and then 18, 25 and 204, while other quotes may not be at all. Quotes — thousands.


Not all that lies in store quotes. In addition to quotes in the repository there are a few (very few units) other values, but their keys are known in advance.

Problem: display random quote from storage.


Prompt, please, the most elegant solution with minimal reshaping of the existing economy.
by | 7 views

3 Answers

0 like 0 dislike
I have a big problem with architecture.
But since you said architecture change is not possible, provided that the known maximum value of the key option could be this (but it's over-not optimal):
\r
do { $str = $m->get(rand(0,$max_key_number)); } while (!$str) echo $str; 
by
0 like 0 dislike
In any case, before you choose, you will have to get all the numbers of the quotations and put them in some place (for example, in a DBMS or a regular PHP array, serialize it and write to the file). Then according to the situation:
\r
1. If the values are stored in a DBMS — do a normal sql query ORDER BY RAND()
2. If the values stored in the array ( for example, $a):
$id = $a[mt_rand(0, count($a)-1]

\r
When adding a new quote, of course, will have to update these lists
by
0 like 0 dislike
It would be logical to store the quotes for the keys of integers from 1 to N
In PHP generete random from 1 to N and remove the key quote, everywhere are doing, if something like that is necessary.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
6 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Mar 24, 2019 by outself
110,608 questions
257,186 answers
0 comments
27,969 users