-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added support for the ATMA resource record
- added initial support for simple local caching. this support for file based, and shared memory based cache. - fixed some typo's in the OPT and TKEY class - added a test for the ATMA RR
- Loading branch information
mike.pultz
committed
Apr 19, 2011
1 parent
abf75ea
commit b1b195a
Showing
14 changed files
with
556 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
class Net_DNS2_Cache | ||
{ | ||
protected $_cache_id = false; | ||
protected $_cache_file = ""; | ||
protected $_cache_data = array(); | ||
protected $_cache_size = 10000; | ||
|
||
public function has($key) | ||
{ | ||
return isset($this->_cache_data[$key]); | ||
} | ||
public function get($key) | ||
{ | ||
if (isset($this->_cache_data[$key])) { | ||
echo "CACHE HIT!\n"; | ||
return $this->_cache_data[$key]['object']; | ||
} else { | ||
|
||
return false; | ||
} | ||
} | ||
public function put($key, $data) | ||
{ | ||
$this->_cache_data[$key] = array('cache_date' => time(), 'object' => $data); | ||
} | ||
protected function clean() | ||
{ | ||
if (count($this->_cache_data) > 0) { | ||
|
||
// | ||
// go through each entry and adjust their TTL, and remove entries that | ||
// have expired | ||
// | ||
$now = time(); | ||
|
||
foreach($this->_cache_data as $key => $data) { | ||
|
||
$diff = $now - $data['cache_date']; | ||
|
||
$this->_cache_data[$key]['cache_date'] = $now; | ||
|
||
foreach($data['object']->answer as $index => $rr) { | ||
|
||
if ($rr->ttl <= $diff) { | ||
|
||
unset($this->_cache_data[$key]); | ||
break 2; | ||
} else { | ||
|
||
$this->_cache_data[$key]['object']->answer[$index]->ttl -= $diff; | ||
} | ||
} | ||
foreach($data['object']->authority as $index => $rr) { | ||
|
||
if ($rr->ttl <= $diff) { | ||
|
||
unset($this->_cache_data[$key]); | ||
break 2; | ||
} else { | ||
|
||
$this->_cache_data[$key]['object']->authority[$index]->ttl -= $diff; | ||
} | ||
} | ||
foreach($data['object']->additional as $index => $rr) { | ||
|
||
if ($rr->ttl <= $diff) { | ||
|
||
unset($this->_cache_data[$key]); | ||
break 2; | ||
} else { | ||
|
||
$this->_cache_data[$key]['object']->additional[$index]->ttl -= $diff; | ||
} | ||
} | ||
} | ||
|
||
// | ||
// how also check to see if we have too many entries, and remove the | ||
// oldest entries first | ||
// | ||
} | ||
} | ||
}; | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
class Net_DNS2_Cache_File extends Net_DNS2_Cache | ||
{ | ||
public function __construct($cache_file) | ||
{ | ||
$this->_cache_file = $cache_file; | ||
if (file_exists($this->_cache_file) == true) { | ||
|
||
// | ||
// open the file for reading | ||
// | ||
$fp = fopen($this->_cache_file, "r"); | ||
if ($fp !== false) { | ||
|
||
// | ||
// lock the file just in case | ||
// | ||
flock($fp, LOCK_EX); | ||
|
||
// | ||
// read the file contents | ||
// | ||
$this->_cache_data = unserialize(fread($fp, filesize($this->_cache_file))); | ||
|
||
// | ||
// unlock | ||
// | ||
flock($fp, LOCK_UN); | ||
|
||
// | ||
// close the file | ||
// | ||
fclose($fp); | ||
|
||
} else { | ||
// throw an exception | ||
} | ||
|
||
$this->clean(); | ||
} | ||
} | ||
public function __destruct() | ||
{ | ||
if (count($this->_cache_data) > 0) { | ||
|
||
// | ||
// open the file for writing | ||
// | ||
$fp = fopen($this->_cache_file, "w"); | ||
if ($fp !== false) { | ||
|
||
// | ||
// lock the file just in case | ||
// | ||
flock($fp, LOCK_EX); | ||
|
||
// | ||
// write the file contents | ||
// | ||
fwrite($fp, serialize($this->_cache_data)); | ||
|
||
// | ||
// unlock | ||
// | ||
flock($fp, LOCK_UN); | ||
|
||
// | ||
// close the file | ||
// | ||
fclose($fp); | ||
|
||
} else { | ||
// throw an exception | ||
} | ||
} | ||
} | ||
}; | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
class Net_DNS2_Cache_Shm extends Net_DNS2_Cache | ||
{ | ||
public function __construct($cache_file, $size) | ||
{ | ||
// | ||
// make sure the file exists first | ||
// | ||
if (!file_exists($cache_file)) { | ||
touch($cache_file); | ||
} | ||
|
||
// | ||
// convert the filename to a IPC key | ||
// | ||
$this->_cache_file = ftok($cache_file, 't'); | ||
echo "using: " . $this->_cache_file . "\n"; | ||
// | ||
// open the shared memory segment | ||
// | ||
$this->_cache_id = shmop_open($this->_cache_file, 'c', 0644, $size); | ||
if ($this->_cache_id !== false) { | ||
|
||
// | ||
// this returns the size allocated, and not the size used, but it's | ||
// still a good check to make sure there's space allocated. | ||
// | ||
$size = shmop_size($this->_cache_id); | ||
if ($size > 0) { | ||
|
||
// | ||
// read the data from teh shared memory segment | ||
// | ||
$data = @shmop_read($this->_cache_id, 0, $size); | ||
if ( ($data !== false) && (strlen($data) > 0) ) { | ||
|
||
// | ||
// unserialize and store the data | ||
// | ||
$this->_cache_data = unserialize($data); | ||
|
||
// | ||
// call clean to clean up old entries | ||
// | ||
$this->clean(); | ||
} | ||
} | ||
} else { | ||
|
||
// throw an exception | ||
} | ||
} | ||
public function __destruct() | ||
{ | ||
if (count($this->_cache_data) > 0) { | ||
|
||
shmop_write($this->_cache_id, serialize($this->_cache_data), 0); | ||
} | ||
|
||
shmop_close($this->_cache_id); | ||
} | ||
}; | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.