我不懂PhP,,,谁能麻烦下,把以下PhP代码转成Java代码。。
谢谢。。。
原代码地址
[php]
<?php
/**
*=------------------------------------------------------------------------=
* InstantRecorder.php
*=------------------------------------------------------------------------=
*
*
* Copyright(c) 2008 by guigui. All rights reserved.
* @author guigui <evan_gui@163.com>
* @version $Id: InstantRecorder.php, v 1.0 2008/4/21 $
* @package systen
* @link http://www.guigui8.com/index.php/archives/108.html
*
* history:
* 1. add this file. (by 桂桂 on 2008/4/21)
*/
define('INSTANT_DATA_SEP', '|g|'); //及时存储的数据 字段间的分隔符
define('MAX_MEM_QUEUE_LENGTH', 1000); //memcached中允许存在记录条数的最大值
/**
*=--------------------------------------------------------------------=
* class InstantRecorder
*=--------------------------------------------------------------------=
*
* 及时数据记录类 的 工场类
*
*/
class InstantRecorder
{
/**
* 获取及时数据记录的对象实例
*
* @param string $mode
* @param string $dir - 最终存储文件时,数据文件被存储的目录路径
* @param string $dataFileSurfix - 最终存储文件时,数据文件名的后缀
* @return InstantMemcachedRecorder or InstantFileRecorder object
*/
static public function getRecorder($mode, $dir = '', $dataFileSurfix = '') {
switch (trim($mode)) {
case 'mem':
return new InstantMemcachedRecorder($dir, $dataFileSurfix);
break;
case 'file':
return new InstantFileRecorder($dir, $dataFileSurfix);
break;
default:
return new InstantFileRecorder($dir, $dataFileSurfix);
}// end of switch
}//end of function
}//end of class
/**
*=--------------------------------------------------------------------=
* class InstantMemcachedRecorder
*=--------------------------------------------------------------------=
*
* 用memcache记录及时数据的处理类
*
* 注:memcache记录的数据,最终要被写到文件中去;
* 写入时机根据memcache设置的缓存队列长度而定。
* 请通过setQueueLength()方法设置队列长度。
*
*/
class InstantMemcachedRecorder
{
private $_host = "127.0.0.1"; //提供memcache服务的主机
private $_port = "11211"; //memcache使用端口号
private $_dataFileSurfix; //数据文件名的后缀
private $_baseDir; //数据文件的目录路径
private $_memcachedObj; //memcached对象句柄
private $_queueLenth; //memcached允许的某个索引对应的记录队列条数的最大值
/**
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
* Public Methods
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
*/
/**
* memcache及时数据记录的对象构造函数
*
* @param string $dir - 最终存储文件时,数据文件被存储的目录路径
* @param string $dataFileSurfix - 最终存储文件时,数据文件名的后缀
* @return InstantMemcachedRecorder object
*/
public function __construct($dir = '', $dataFileSurfix = '') {
if (empty($dir)) {
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
if (empty($dataFileSurfix)) {
$dataFileSurfix = '_noname.txt';
}
$this->setBaseDir($dir);
$this->setDataFileSurfix($dataFileSurfix);
$this->_queueLenth = MAX_MEM_QUEUE_LENGTH;
}
/**
* 设置memcache缓存队列的长度
*
* @param unknown_type $len
* @return unknown
*/
public function setQueueLength($len=0) {
$len = intval($len);
if ($len < 1) return false;
$this->_queueLenth = $len;
}
/**
* 设置数据文件的文件缀名
*
* @param unknown_type $str
*/
public function setDataFileSurfix($str) {
$this->_dataFileSurfix = $str;
}
/**
* 设置数据文件名
*
* @param unknown_type $str
*/
public function setDateFileName($str) {
$this->_dateFileName = $str;
}
/**
* 设置数据文件的目录路径
*
* @param unknown_type $dir
*/
public function setBaseDir($dir) {
$this->_baseDir = $dir;
}
/**
* 将数据进行及时记录
*
* @param string $data
* @return boolean
*/
public function record($data) {
//
// 1. detect and initiate memcache module, if not found, the then use file writing strategy.
//
if (!class_exists('Memcache') || !function_exists('memcache_connect')) {
$file_recorder = new InstantFileRecorder($this->_baseDir, $this->_dataFileSurfix);
return $file_recorder->record($data);
} else {
$this->_initMemcached();
}
//
//2. write data into memcache.
//
$data_key_prefix = trim($this->_baseDir) . trim($this->_dataFileSurfix);
$num_key = $data_key_prefix . '_num';
$cur_queue_num = intval($this->_memcachedObj->get($num_key));
$data_key = $data_key_prefix . $cur_queue_num;
$this->_memcachedObj->set($data_key, $data . "\r\n");
if ($cur_queue_num >= $this->_queueLenth - 1) {
//if the record time surplus 23:55, we write all memcached data into file.
$this->_writeIntoFile($data_key_prefix, $this->_queueLenth);
$this->_memcachedObj->set($num_key, 0);
} else {
$this->_memcachedObj->set($num_key, $cur_queue_num + 1);
//if the record time surplus 23:55, we write all memcached data into file.
if (intval(date('Hi')) >= 2355) {
$this->_writeIntoFile($data_key_prefix, $cur_queue_num + 1);
$this->_memcachedObj->set($num_key, 0);
}
}
return true;
}
/**
* 将所有通过本类处理的所有memcache数据,立即写入到文件
*
* @return unknown
*/
public function clearMemData() {
$this->_initMemcached();
$data_key_prefix = trim($this->_baseDir) . trim($this->_dataFileSurfix);
return $this->_writeIntoFile($data_key_prefix);
}
/**
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
* Private Methods
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
*/
/**
* 初始化memcache对象句柄
*
*/
private function _initMemcached() {
$this->_memcachedObj = new Memcache();
$this->_memcachedObj->connect($this->_host, $this->_port)
or die ("Could not connect memcached server!");
}
/**
* 将memcache缓存的数据 立即 写入到指定的 文件
*
* @param string $data_key_prefix - 被写入的memcache数据key的前缀名
* @param integer $flush_queue_len - 设置写入的队列长度(默认为0,表示都写入)
*/
private function _writeIntoFile($data_key_prefix, $flush_queue_len=0) {
$flush_queue_len == 0 && $flush_queue_len = $this->_queueLenth;
$file_recorder = new InstantFileRecorder($this->_baseDir, $this->_dataFileSurfix);
$data = '';
for ($i = 0; $i < $flush_queue_len; $i++) {
$data_key = $data_key_prefix . $i;
$data .= $this->_memcachedObj->get($data_key);
$this->_memcachedObj->set($data_key, '');
}
$data = substr($data, 0, -2);
$file_recorder->record($data);
}
private function _needFlush() { }
}//end of class