पीएचपी जादू तरीकों है:
__get($property)
हमें एक वस्तु पर एक $ संपत्ति की पहुँच को लागू
__set($property, $value)
हमें एक वस्तु पर एक $ संपत्ति की नियुक्ति को लागू करने देते हैं
PHP चर को क्रमबद्ध कर सकता है:
serialize($variable)
रिटर्न चर
unserialize($string)
रिटर्न की एक स्ट्रिंग प्रतिनिधित्व एक स्ट्रिंग से एक चर वापस
पीएचपी समवर्ती का उपयोग प्रबंधन के साथ फ़ाइलें, संभाल कर सकते हैं:
fopen($file, 'c+')
सलाहकार ताला विकल्पों के साथ एक फ़ाइल को खोलता है सक्षम (आपको झुंड का उपयोग करने की अनुमति देता है)
flock($descriptor, LOCK_SH)
एक साझा लॉक लेता है (पढ़ने के लिए)
flock($descriptor, LOCK_EX)
एक ई लेता है xclusive ताला (writting के लिए)
तो, क्षुधा के बीच एक वस्तु साझा करने के लिए सबसे आसान तरीका है एक वर्ग है कि औजार और उन सभी सामान का उपयोग एक फ़ाइल में अपने सभी डेटा को बचाने और तुरंत बहाल करने के लिए तैयार करना है।
उस वर्ग का एक सरल कार्यान्वयन हो सकता है:
class Synchro
{
private $_file;
public function __construct($file)
{
$this->_file = $file;
}
public function __get($property)
{
// File does not exist
if (!is_file($this->_file))
{
return null;
}
// Check if file is readable
if ((is_file($this->_file)) && (!is_readable($this->_file)))
{
throw new Exception(sprintf("File '%s' is not readable.", $this->_file));
}
// Open file with advisory lock option enabled for reading and writting
if (($fd = fopen($this->_file, 'c+')) === false)
{
throw new Exception(sprintf("Can't open '%s' file.", $this->_file));
}
// Request a lock for reading (hangs until lock is granted successfully)
if (flock($fd, LOCK_SH) === false)
{
throw new Exception(sprintf("Can't lock '%s' file for reading.", $this->_file));
}
// A hand-made file_get_contents
$contents = '';
while (($read = fread($fd, 32 * 1024)) !== '')
{
$contents .= $read;
}
// Release shared lock and close file
flock($fd, LOCK_UN);
fclose($fd);
// Restore shared data object and return requested property
$object = json_decode($contents);
if (property_exists($object, $property))
{
return $object->{$property};
}
return null;
}
public function __set($property, $value)
{
// Check if directory is writable if file does not exist
if ((!is_file($this->_file)) && (!is_writable(dirname($this->_file))))
{
throw new Exception(sprintf("Directory '%s' does not exist or is not writable.", dirname($this->_file)));
}
// Check if file is writable if it exists
if ((is_file($this->_file)) && (!is_writable($this->_file)))
{
throw new Exception(sprintf("File '%s' is not writable.", $this->_file));
}
// Open file with advisory lock option enabled for reading and writting
if (($fd = fopen($this->_file, 'c+')) === false)
{
throw new Exception(sprintf("Can't open '%s' file.", $this->_file));
}
// Request a lock for writting (hangs until lock is granted successfully)
if (flock($fd, LOCK_EX) === false)
{
throw new Exception(sprintf("Can't lock '%s' file for writing.", $this->_file));
}
// A hand-made file_get_contents
$contents = '';
while (($read = fread($fd, 32 * 1024)) !== '')
{
$contents .= $read;
}
// Restore shared data object and set value for desired property
if (empty($contents))
{
$object = new stdClass();
}
else
{
$object = json_decode($contents);
}
$object->{$property} = $value;
// Go back at the beginning of file
rewind($fd);
// Truncate file
ftruncate($fd, strlen($contents));
// Save shared data object to the file
fwrite($fd, json_encode($object));
// Release exclusive lock and close file
flock($fd, LOCK_UN);
fclose($fd);
return $value;
}
}
अब, आप जब निर्माण stdClass
की तरह इस वर्ग का उपयोग कर सकते हैं, लेकिन एक फ़ाइल पथ के साथ।
$obj = new Synchro("/tmp/test.sync");
$obj->hello = 'world';
// ... and in another process...
echo $obj->hello;
यह उदाहरण, बहुत ही सरल निश्चित रूप से है कि यह एक फाइल करने के लिए समवर्ती पहुँच के बारे में है, लेकिन एक चर के लिए नहीं ख्याल रखता है, एक बेहतर कार्यान्वयन में आप एक म्युटेक्स की तरह ताला इस्तेमाल करेंगे।
मैंने इस कक्षा को गिटूब पर (इसे पूरा करने के बाद) धक्का दिया, तो आप इसे here पा सकते हैं।
बड़ा है कि आपके सरणी है - साझा स्मृति ब्लॉक के आकार
हाँ। क्या आप थोड़ा और संदर्भ जोड़ सकते हैं? डेटाबेस में डेटा संग्रहीत करने के बारे में, और वर्तमान स्क्रिप्ट में केवल बिट्स को पुनर्प्राप्त करने के बारे में कैसे? –
यह आमतौर पर डेटाबेस में डेटा संग्रहीत करके हल किया जाता है, और केवल वही चीज़ प्राप्त करता है जो आवश्यक है। –