Using MYsql 5.6 Memcache

It seems to me that I am missing something really obvious, but I'm trying to use MySQL 5.6 and return values ​​via memcache

So, I configured MYSQL to use the memcache plugin, configured the details in the innodb_memcache.containers table

Now I have two elements in this table: by default they are entered by MySQL and my own settings, both of them have table names.

To get data through php, I use:

$memcache->get($key); 

Where $ key is the data in the db column

However, this does not return anything, I suspect that the reason is that according to the MySQL docs, if the table name is not specified, it selects the first in the list, which is not the one I want, that I don’t understand how I specify the correct name tables in the key, so he knows which table to look for the key in.

Additional Information:

 table design: table: codes id INT PK code VARCHAR UNIQUE codeval VARCHAR innodb_memcache.containers : name: mycode db_schema: databaseName db_table: codes key_columns: code value_columns: codeval flags: id cas_column: null expire_time_column: null unique_idx_name_on_key: code 

code:

 $table = "mycode"; $key = "123456"; $memcache = new Memcache; $memcache->connect($this->CONNECTURL, $this->CONNECTPORT) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server version: ".$version."<br/>\n"; $key = "@@" . $table . "." . $key . "." . $table; $get_result = $memcache->get($key); print_r($get_result); 

The above code returns the server version without any problems, so the connection works. print_r ($ get_result) returns a null value when it should return a value

It throws a notification: attempt to get a non-object property

So, if someone can tell me how I specify the $ key, which table I use for querying through memcache, I would be very grateful!

+4
source share
4 answers

The table name ( table_id in @@table_id ) should be the value from your mappings ( innodb_memcache.containers ), and not the actual table name if this changes.

And if the table name in the mycode , then the received request through memcache should look like this:

 $table = 'mycode'; $key = '123456'; $memcache->get( '@@' . $table . '.' . $key ); 

In the end there is no extra '.' . $table '.' . $table '.' . $table .

Some details are available on the InnoDB memcached plugin scheduler documentation page.

To name a few important here:

  • Use select * from innodb_memcache.containers; to get specific mappings;
  • Pay attention to the organization of requests:

For example, @@ t1.some_key and @@ t2.some_key have the same key value, but are stored in different tables and therefore do not conflict.

+3
source

From: http://dev.mysql.com/doc/refman/5.6/en/innodb-memcached-intro.html

Namespaces: memcached is like one giant directory, where files do not conflict with each other, you can give them complex names with prefixes and suffixes. The built-in InnoDB / memcached server allows you to use the same naming conventions for one-key keys. Keyword names of the @@ table_id.key.table_id format are decoded to refer to a specific table using matching data from the innodb_memcache.containers table. The key is viewed or written to the specified table.

The @@ notation only works for individual calls to the get, add, and set functions, and not others, such as incr or delete. To assign a default table for all subsequent memcached operations in a session, run a retrieval request using the @@ notation and the table identifier, but without the key part. For instance:

get @@ table_x

Subsequent get, set, incr, delete, and other operations use the table designated by table_x in the innodb_memcache.containers.name column.

+4
source

If you still have default tables, you can try using telnet.

Note. This was used on an AWS RDS instance with memcached, it should be the same for any MySQL implementation using memcached, but I'm not sure.

 telnet localhost 11211 stats #=> should return a long list of stats including pid, uptime, etc get AA #=> should return VALUE AA 8 12 HELLO, HELLO END quit #exit telnet session 

I know this does not answer your question, but may help in troubleshooting.

0
source
 <?php $memc = new Memcache; $memc->addServer('localhost','11211'); if(empty($_POST['film'])) { ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Simple Memcache Lookup</title> </head> <body> <form method="post"> <p><b>Film</b>: <input type="text" size="20" name="film"></p> <input type="submit"> </form> <hr/> <?php } else { echo "Loading data...\n"; $film = htmlspecialchars($_POST['film'], ENT_QUOTES, 'UTF-8'); $mfilms = $memc->get($film); if ($mfilms) { printf("<p>Film data for %s loaded from memcache</p>", $mfilms['title']); foreach (array_keys($mfilms) as $key) { printf("<p><b>%s</b>: %s</p>", $key, $mfilms[$key]); } } else { $mysqli = mysqli('localhost','sakila','password','sakila'); if (mysqli_connect_error()) { sprintf("Database error: (%d) %s", mysqli_connect_errno(), mysqli_connect_error()); exit; } $sql = sprintf('SELECT * FROM film WHERE title="%s"', $mysqli->real_escape_string($film)); $result = $mysqli->query($sql); if (!$result) { sprintf("Database error: (%d) %s", $mysqli->errno, $mysqli->error); exit; } $row = $result->fetch_assoc(); $memc->set($row['title'], $row); printf("<p>Loaded (%s) from MySQL</p>", htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8'); } } ?> </body> </html> 

With PHP, connections to memcached instances remain open as long as PHP and its associated Apache instance work. When adding or removing servers from the list in the running instance (for example, when starting another script that mentions additional servers), the connections are common, but the script selects only the instances explicitly configured in the script.

-2
source

Source: https://habr.com/ru/post/1489842/


All Articles