I am using memcache for caching layer and i connect to it using php memcache client.
So if i need to fetch 2 keys then i can do this in following two ways:
First:
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var1 = $memcache_obj->get('key_one');
$var2 = $memcache_obj->get('key_two');
Second
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get(Array('key_one', 'key_two'));
So my question is will i get any performance benefit in the second approach or internally php memcache client still make two connection requests to memcache server?
On memcache command level we can get multiple keys in a single command but not sure php client.
Related
I am using google client API to fetch status of my instances to make a local database copy.
It is possible that multiple scrips update my local copy. I could fetch the data, and while the data is travelling back to my server, some other script would modify the data. After I store the data from original fetch, a lost update is created.
Therefore I need to use transactions to block all other traffic to my table while I making making an update
This is the code for fetching:
<?php
require_once './gcloud/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('Google-ComputeSample/0.1');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$project = 'project_id'; // TODO: Update placeholder value.
$zone = 'us-east1-b'; // TODO: Update placeholder value.
$instance = 'instance-1'; // TODO: Update placeholder value.
$mysqli = new mysqli($hn, $un, $pw, $db);
$mysqli->begin_transaction();
$listInstancesJSON = $service->instances->listInstances($project, $zone, []);
//store it
$mysqli->commit();
Blocking table while making a request sounds like a terrible idea. I think I'll add ini_set('max_execution_time', 5); at the start of the script, just in case fetch fails (I presume they use curl). In case of execution time exceeding 5s, would my table (or database) remain blocked even after script termination? Is there any other defence mechanism I should implement?
I plan to run this code as a cron job every minute.
It sounds like listInstances needs the equivalent of
SELECT ... FOR UPDATE
I'm having issue using the SDK for PHP.
If I try to estabilish a connection it takes ages...
Here some code:
$old = microtime (true);
$db = new Couchbase(...);
echo microtime (true)-$old."
";
$old = microtime (true);
$db->get(...);
echo microtime (true)-$old;
The output is this:
2.2835459709167 (couchbase establishing)
0.0011978149414062 (get command)
Why does the connection to couchbase take so long time?
The initial connection does take a while, but there is a flag for using persistent connections with the Couchbase() object. It's the last parameter. Generally, it's a good idea to set it to true.
The project is considering setting it to true by default in a future release.
Check which value you're using for the server host, if you use, for example:
$cb = new Couchbase("couchbase_hostname:8091", "user", "pass", "default" , true);
the issue may be the DNS resolution for "couchbase_hostname", try passing the host IP, you didn't paste the whole script code so I can't tell which value you're passing.
I have a page where few thousands of users can hit a method at the same time . I do have following code where I connect every time . Since this will go to a seperate memcache server will this cause slowdowns is there a way to connect just once and reuse that connection ? Do I have to close connection after every request ?
$primary_connected = $memcache_primary->connect($primary_memcache_server, 11211);
if($primary_connected){
$data = $memcache_primary->get($key);
if ($data != NULL) {
return data;
}
}
else{
/////Get data from database
}
If you are using the PHP memcached class (the one with the d on the end, not memcache) then yes, you can open a persistent connection.
You can pass a persistent ID to the constructor which will open a persistent connection and subsequent instances that use the same persistent ID will use that connection.
$memcached = new Memcached('method_name_or_persistent_identifier');
$memcached->addServer(...);
// use it
Hope that helps.
See Memcached::__construct() for more details.
Here is the php code that I'm working with in my local machine:
$m = new Mongo();
$db=$m->selectDB("def");
//then all in my code i use $db to select insert ... (as defined in php doc)
Now I want to connect my application to a remote server (hosted by mongood.com)
How can I do this?
You can use mongoOd without the REST API
But remember, it's a replica Set cluster so You need to configure your PHP for a ReplicaSet configuration...
I use mongoOd within ruby & mongoid (not the REST API)
Here a php example
<?php
// connecting to mongood.com cluster
$m = new Mongo("mongodb://94.23.54.103:27017,188.165.219.99:27017,94.23.220.151:27017", array("replicaSet" => "cluster"));
var_dump($m);
$db = $m->selectDB('my_database');
$db->authenticate("my_login", "my_password");
$collection = new MongoCollection($db, 'my_collection');
$cursor = $collection->find();
foreach ($cursor as $doc) { var_dump($doc); }
?>
Enjoy :)
A mongoOd Team member
The constructor for the mongo object takes as its arguments connection parameters.
http://www.php.net/manual/en/mongo.construct.php
$m = new Mongo('mongodb://[username:password]#host:port')
You'll have to ask them what the connection URI is, and then use:
$m = new Mongo("mongodb://username:password#hostname");
However, I am not sure if that option is available to you. Their website says you can access data via a REST API.
At any rate, you should ask them for help. There's a button on the left that reads "aide," if you click on it you'll get a form where you can fill in your email and your question.
Reference: Mongo - Connecting
I am benchmarking an app I made which uses repcached (memcached with replication) for storing objects and taking some load off the db.
While benchmarking the index page I run
ab -c 400 -n 5000 http://mysite
When I use just one memcache server with
list($server, $port) = explode(':', $settings->memcached_servers[0]);
$this->link = new Memcache();
$this->link->connect($server, (int) $port);
I get 1000 reqs/sec
When I out more than one server to the pool with
$this->link = new Memcache();
foreach($settings->memcached_servers as $server){
list($server, $port) = explode(':', $server);
$this->link->addServer($server, (int) $port, 0, 10);
}
I only get 300 reqs/sec
The difference is huge
Any idea why?
I really need to have 2 server for redundancy but the performance is also crucial
It it normal to have such a huge difference?
Basically, the index page makes justs 2 calls to the db getting just one row, so the rows are cached while running the test.
But I am surprised to see memcached fall so much behind in the test.
Well it seems that the culprit was the weight parameter of the addServer
Changing it to 1 for the 1st server and 2 for the second did it and performance is now the same