hi i want to know why i cant store multi dimensional(array size is more than 1000)
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
the above s working correctly...
the below one have error...
$memcache->set('key', $sear, false, 60) or die ("Failed to save data at the server");
if the $sear is string or object array then no problem for store data at the server..
but i store multi dimensional array in memcached,,i will get the error is
Failed to save data at the server
thanks and advance
The array you're trying to store is probably too large. Memcache has a limit on the size of a single item. The maximum size per item is 1,048,576 Bytes or 1MB.
Here is another thread regarding this issue...
Can you store a PHP Array in Memcache?
Related
I'm having problem with query caching in files. I have a code which is trying to get file with serialized query result and if file is not found, or older then one day then I'm trying to connect to DB and get it remotely. Code looks like this:
// Getting cache file function
$cur_time = #time();
$time_modified = #filemtime($file_path);
if ($cur_time > ($time_modified + $cache_period_hours))
return NULL;
else
return unserialize(file_get_contents($file_path));
//If NULL is returned get result remotely;
if($result === NULL){
if (!db::connect("base", "192.168.1.111", "root", "password"))
die("Database error. Can not connect to database.");
//Execute query, write to file serialized result
}
When I have huge traffic I get this error:
Lost connection to MySQL server at 'reading authorization packet', system error: 0.
But I don't know why the script is even trying to connect to database when I have fresh cached file. Is there a limit on how many users can read same file at same time? Also is there any way to prevent this MySQL error?
Change to:
if ($cur_time > ($time_modified + $cache_period_hours*3600))
This will convert $cache_period_hours to seconds, to match the units of the other variables.
I have 3Mb string data in mysql column. When I run a SQL query at mysql prompt it returns complete data. But from php I am unable to fetch complete data, it is returning only 1mb of data data.
my mysql configs have max_allowed_packet_size=33554432
PHP max_post_size=30mb and memory_limit=300mb
Are you using PDO?
The problem may be that the insert buffer default value is 1MiB.
To change it, update the line used to connect to the database by adding the new value for the parameter MYSQL_ATTR_MAX_BUFFER_SIZE:
$pdo = new PDO("your connection_settings", "user", "pass", array(PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>1024*1024*5));
In this example, the new size of the buffer will be 5MiB.
See this page for more information about this attribute.
I am curious is there a possibility in PHP to copy key/value from one memcached server directly to another using Memcached module?
Is connections to 2 different servers at one time allowed at all?
Thanks in advance!
The following would allow you to connect to two different Memcached servers and set the same data on both:
//Server A
$memcacheA = new Memcache;
$memcacheA->connect(216.239.51.99, 11211) or die ("Could not connect");
//Server B
$memcacheB = new Memcache;
$memcacheB->connect(115.239.51.98, 11211) or die ("Could not connect");
//Getting data from your database.
$myVal = $customObj->getSomethingFromDB();
//If data not stored on Server A
if($memcacheA->get('var_key') === false){
//Store it on Server A
$memcacheA->set('var_key', $myVar, MEMCACHE_COMPRESSED, 50);
}
//If data not stored on Server B
if($memcacheB->get('var_key') === false){
//Store it on Server B
$memcacheB->set('var_key', $myVar, MEMCACHE_COMPRESSED, 50);
}
Depending on your use case, this may or may not be a good solution. Depends on what your situation is and what you're attempting to achieve.
I have an events calendar.
Each event has a different location on the calendar.
Every time page loads, I empty the event location table and I write the location of the event to the table.
event location table looks like this
eventid location
1 1
2 2
3 1
4 1
5 1
6 2
7 3
8 4
9 5
There are some other variables involved but it is pretty much like this. Each event needs to know the previous event's location so that it can decide its own location.
There are also different views of this calendar and each view use the same technique so it is not the healthiest way to solve this.
The question is;
Is there any way to store this table's data on a cache or something like that instead of re-writing the table every single time.
There are at least two approaches you can take to cache this data. The easiest is to serialize() the data and store it in your database. When you need to retrieve the database, query it from the database, unserialize() it, and use it as before.
As second approach is to add memcache to your PHP installation and access your data via the memcache functions. Example:
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>
I'm trying to add some speed performance to a project I'm working on using memcache. However I'm having a problem with the following
public function sql_query($query){
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memkey = md5($query);
$get_result = $memcache->get($memkey);
if ($get_result){
return $get_result;
} else {
$q = $this->mysqli->query($query);
$memcache->set($memkey, $q, false, 120) or die ("Failed to save data at the server");
if ($this->mysqli->error){
App::Error($this->mysqli->error);
}
return $q;
}
}
It is certainly putting something into memcached but I get this error when pulling it back out and use it as I would if it wasn't from the cache.
"Warning: mysqli_result::fetch_assoc(): Couldn't fetch mysqli_result"
Am I missing something? I'm sure I've used memcache before in a similar way without any issues.
You can not store all data-types into memcached (and other data-stores), one which most often does not work are resources Docs, for example a database link or a result identifier.
You stored such a value and on another request you pulled it out again. But as the database already is in another state, this resource does not work any longer. You get the error then.
Instead you need to store the result data from the query, that's static data you can put into memcache.