Using MYsql 5.6 Memcache

社会主义新天地 提交于 2020-01-03 18:37:09

问题


I must be missing something really obvious here I think, but what I am trying to do is use MySQL 5.6 and return values through memcache

So I have set up MYSQL to use the memcache plugin, set up the details in the innodb_memcache.containers table

I now have two items in that table, the default ones entered by MySQL and my own settings, both of them have table names.

To get the data via php I use:

  $memcache->get($key);

Where $key is the data in the db column

However this returns nothing, I suspect the reason is that, according to the MySQL Docs if no table name is specified, it choose the first one in the list, which is not the one I want, what I don't understand is how I specify the correct table name in the key, so it 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's 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 issue, so the connection is working. print_r($get_result) returns blank, when it should be returning a value

It does throw a notice: Trying to get property of non-object

So if someone could let me know how I specify with the $key which table I am using to query through memcache, I would be much appreciated!


回答1:


The table name (table_id in @@table_id) must be the value from your mappings (innodb_memcache.containers), not the actual table name, if that varies.

And if you table name in mappings is mycode, then the resulting query through memcache should look like this:

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

There is no extra '.' . $table at the end.

Some details are available from InnoDB memcached Plugin documentation page.

To name a few of importance here:

  1. Use select * from innodb_memcache.containers; to get defined mappings;
  2. Note the queries organization:

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




回答2:


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

Namespaces: memcached is like a single giant directory, where to keep files from conflicting with each other you might give them elaborate names with prefixes and suffixes. The integrated InnoDB / memcached server lets you use these same naming conventions for keys, with one addition. Key names of the format @@table_id.key.table_id are decoded to reference a specific a table, using mapping data from the innodb_memcache.containers table. The key is looked up in or written to the specified table.

The @@ notation only works for individual calls to the get, add, and set functions, not the others such as incr or delete. To designate the default table for all subsequent memcached operations within a session, perform a get request using the @@ notation and a table ID, but without the key portion. For example:

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.




回答3:


If you still have the 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 doesn't answer your question, but it might help in troubleshooting.




回答4:


<?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, the connections to the memcached instances are kept open as long as the PHP and associated Apache instance remain running. When adding or removing servers from the list in a running instance (for example, when starting another script that mentions additional servers), the connections are shared, but the script only selects among the instances explicitly configured within the script.



来源:https://stackoverflow.com/questions/17481866/using-mysql-5-6-memcache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!