喜悦国际村 » 代码共享 » 最近写的代码 仅做交流用

页: 1 [2] 3 4
1314ing2007-6-4 01:17 PM
收了

另:+1 喜悦币

rjp_ww2007-6-5 01:33 AM
LZ真厉害,这么多代码,看的我头疼,一定好好研究!

oghuz2007-6-6 05:13 AM
不错
学习了

looking_for12007-6-18 02:50 AM
赞赞

LZ,你的那个HTTPRequest怎么这么象ajax方法啊。
开始猜想,php以后能囊括ajax吗

ml1882007-6-27 09:20 AM
ding..............

szphper2007-7-4 02:26 AM
rfc http1.1

sanders_yao2007-7-6 09:23 AM
回复 #25 looking_for1 的帖子 & 回复 #27 szphper 的帖子

还没有js的那个xmlHttpRequest对象那么强
不过都是发请求 只不过一个从客户端发 一个从服务器端发

rfc 我还没有仔细研究过 唯一仔细读了的rfc文档是[url]http://www.faqs.org/rfcs/rfc1867.html[/url] 这篇 用来模拟上传文件的

sanders_yao2007-7-6 09:32 AM
今天刚完成的一个缓存类型

今天刚完成的一个缓存类型
这个类型来自自己的类型库计划:oomph(性感)
正在写的一个个人项目叫:C-CUP CMS(C罩杯CMS)目的是在实际应用中检测并充实oomph库中的各个代码

[php]
<?php
/*=============================================
#        类型名称                        : 缓存类型
#        作者                                : Sanders Yao
#        文件名                        : cache.php
#        作用                                : 缓存数据
#        最后修改时间                : 2007-7-6
#        @itsDir                        : 缓存数据所在绝对路径
#        @itsList                : 缓冲列表
#        @itsConfig                : 配置数据
=============================================*/
define("CACHE_CONFIG_FILE", "config");        //配置文件的文件名
define("CACHE_DEFAULT_PERFIX", "c_");        //数据文件的前缀(需要有别于配置文件的文件名的前几个字符)

class CCupCache{
        var $itsDir;
        var $itsList;
        var $itsConfig;
       
        /*=============================================
        #        作用                : 构造函数
        #        @dir        : 缓存文件所在目录
        =============================================*/
        function CCupCache($dir = ""){
                $this->itsDir                = $dir;
                $this->itsList                = array();
                $this->itsConfig        = array();
                $this->ini();
        }
       
        /*=============================================
        #        作用                : 初始化缓存(如果存在config.php文件 则加载缓存配置数据)
        #        return        : 执行结果
        =============================================*/
        function ini(){
                if(!is_dir($this->itsDir)){
                        return false;
                }
                $this->itsDir = false !== strpos($this->itsDir, "./") ? str_replace("\\", "/", realpath($this->itsDir)) : $this->itsDir;
                if(is_file($this->itsDir . "/" . CACHE_CONFIG_FILE . ".php")){
                        $this->itsConfig        = $this->get(CACHE_CONFIG_FILE, "");
                }
                return true;
        }
       
        /*=============================================
        #        作用                : 获取缓存数据
        #        @key        : 缓存的键值
        #        @perfix        : 缓存前缀(默认CACHE_DEFAULT_PERFIX)
        #        return        : 缓存数据或false
        =============================================*/
        function & get($key, $perfix = CACHE_DEFAULT_PERFIX){
                if(isset($this->itsList[$perfix . $key]) && NULL != $this->itsList[$perfix . $key]){
                }elseif(is_file($this->itsDir . "/" . $perfix . $key . ".php")){
                        include($this->itsDir . "/" . $perfix . $key . ".php");
                        $this->itsList[$perfix . $key] = $cacheValue;
                }else{
                        $this->itsList[$perfix . $key] = NULL;
                }
                return $this->itsList[$perfix . $key];
        }
       
        /*=============================================
        #        作用                        : 设定缓存数据
        #        @key                : 缓存的键值
        #        @value                : 缓存的变量值
        #        @perfix                : 缓存前缀(默CACHE_DEFAULT_PERFIX)
        #        @config                : 设定数据(一个关联数组 键值为"ttl", "rebuild", "priority", "last", "description"中的一个或多个)
        #        @is_config        : 当前数据是否是当前缓存的配置数据
        #        return                : 执行结果
        #        Attention!!        : @is_config为false的情况下@perfix.@key不能等于 CACHE_CONFIG_FILE!!
        =============================================*/
        function set($key, $value, $perfix = CACHE_DEFAULT_PERFIX, $config = NULL, $is_config = false){
                if(CACHE_CONFIG_FILE == $perfix . $key && !$is_config){
                        return false;
                }elseif($is_config){
                        $key        = CACHE_CONFIG_FILE;
                        $value        = $this->itsConfig;
                        $perfix        = "";
                        $config        = NULL;
                }
                $filePath = $this->itsDir . "/" . $perfix . $key . ".php";
                if(false !== $fp = fopen($filePath, "w")){
                        $content = "<?php\n\$cacheValue = " . var_export($value, true) . "\n?>";
                        flock($fp, LOCK_EX);
                        fwrite($fp, $content, strlen($content));
                        fclose($fp);
                        $this->itsList[$perfix . $key] = $value;
                        if(NULL != $config){
                                $ttl                        = isset($config["ttl"])                        ? $config["ttl"]                        : NULL;
                                $rebuild                = isset($config["rebuild"])                ? $config["rebuild"]                : NULL;
                                $priority                = isset($config["priority"])        ? $config["priority"]                : NULL;
                                $last                        = isset($config["last"])                ? $config["last"]                        : NULL;
                                $description        = isset($config["description"])        ? $config["description"]        : NULL;
                                $this->setConfig($key, $perfix, $ttl, $rebuild, $priority, $last, $description);
                        }
                        return true;
                }
                return false;
        }
       
        /*=============================================
        #        作用                : 查询缓存设定
        #        @key        : 缓存的键值
        #        @perfix        : 缓存前缀(默认CACHE_DEFAULT_PERFIX)
        #        @fuzzy        : 模糊查询开关
        #        return        : 查询结果
        =============================================*/
        function getConfig($key = NULL, $perfix = CACHE_DEFAULT_PERFIX, $fuzzy = false){
                $config = array();
                if(NULL == $key){
                        $config = $this->itsConfig;
                }elseif($fuzzy){
                        foreach($this->itsConfig as $k => $c){
                                if(false !== strpos($c["name"], $key)){
                                        $config[] = $c;
                                }
                        }
                }elseif(isset($this->itsConfig[$perfix . $key])){
                        $config[] = $this->itsConfig[$perfix . $key];
                }
                return $config;
        }
       
        /*=============================================
        #        作用                        : 设定缓存设置
        #        @key                : 缓存的键值
        #        @perfix                : 缓存前缀(默认CACHE_DEFAULT_PERFIX)
        #        @ttl                : 更新周期
        #        @rebuild        : 重建缓存脚本文件地址
        #        @priority        : 更新优先级
        #        @last                : 上次更新结束的时间(时间戳)
        #        @description: 说明文本
        =============================================*/
        function setConfig($key, $perfix = CACHE_DEFAULT_PERFIX, $ttl = NULL, $priority = NULL, $rebuild = NULL, $last = NULL, $description = NULL){
                $this->itsConfig[$perfix . $key] = isset($this->itsConfig[$perfix . $key]) ? $this->itsConfig[$perfix . $key] : array();
                $this->itsConfig[$perfix . $key]["name"]                        = $key;
                if(NULL === $last && !isset($this->itsConfig[$perfix . $key]["last"])){
                        $this->itsConfig[$perfix . $key]["last"]                = time();
                }elseif(NULL !== $last){
                        $this->itsConfig[$perfix . $key]["last"]                = $last;
                }
                if(NULL === $priority && !isset($this->itsConfig[$perfix . $key]["priority"])){
                        $this->itsConfig[$perfix . $key]["priority"]        = count($this->itsConfig[$perfix . $key]);
                }elseif(NULL !== $priority){
                        $this->itsConfig[$perfix . $key]["priority"]        = $priority;
                }
                if(NULL === $description && !isset($this->itsConfig[$perfix . $key]["description"])){
                        $this->itsConfig[$perfix . $key]["description"]        = "";
                }elseif(NULL !== $description){
                        $this->itsConfig[$perfix . $key]["description"]        = $description;
                }
                $this->itsConfig[$perfix . $key]["ttl"]                                = intval($ttl);
                $this->itsConfig[$perfix . $key]["rebuild"]                        = is_file($rebuild) ? str_replace("\\", "/", realpath($rebuild)) : NULL;
        }
       
        /*=============================================
        #        作用                : 保存缓存设置
        #        return        : 执行结果
        =============================================*/
        function saveConfig(){
                return $this->set(NULL, NULL, NULL, NULL, true);
        }
       
        /*=============================================
        #        作用                : 根据配置数据更新缓存
        #        return        : 更新缓存的数量
        =============================================*/
        function update(){
                $updateStatus = 0;
                uasort($this->itsConfig, "cachePriority");
                foreach($this->itsConfig as $k => $c){
                        if($c["last"] + $c["ttl"] <= time() && is_file($this->itsDir . "/" . $k . ".php")){
                                list($usec, $sec) = explode(" ", microtime());
                                $startTime = (float)$usec + (float)$sec;
                               
                                unlink($this->itsDir . "/" . $k . ".php");
                                if(isset($c["rebuild"]) && is_file($c["rebuild"])){
                                        extract($GLOBALS);
                                        include($c["rebuild"]);
                                }
                               
                                list($usec, $sec) = explode(" ", microtime());
                                $endTime = (float)$usec + (float)$sec;
                                $this->itsConfig[$k]["last"] = time();
                                $this->itsConfig[$k]["runtime"] = $endTime - $startTime;
                                $updateStatus ++;
                        }
                }
                if($updateStatus){
                        $this->saveConfig();
                }
                return $updateStatus;
        }
       
        /*=============================================
        #        作用                : 删除单个缓存
        #        return        : 执行结果
        =============================================*/
        function delete($key, $perfix = CACHE_DEFAULT_PERFIX){
                if(!is_file($this->itsDir . "/" . $perfix . $key . ".php")){
                        return false;
                }
                unlink($this->itsDir . "/" . $perfix . $key . ".php");
                if(isset($this->itsConfig[$perfix . $key])){
                        $this->itsConfig[$perfix . $key]["last"] = time();
                }
                return true;
        }
       
        /*=============================================
        #        作用                : 删除单个缓存
        #        return        : 执行结果
        =============================================*/
        function distroy($key, $perfix = CACHE_DEFAULT_PERFIX){
                if(!is_file($this->itsDir . "/" . $perfix . $key . ".php")){
                        return false;
                }
                unlink($this->itsDir . "/" . $perfix . $key . ".php");
                if(isset($this->itsConfig[$perfix . $key])){
                        unset($this->itsConfig[$perfix . $key]);
                }
                return true;
        }
}

/*=============================================
#        作用                        : 缓存优先级排序的回调函数
#        @a                        : 配置数据a
#        @b                        : 配置数据b
#        return                : 比较结果
=============================================*/
function cachePriority($a, $b){
        if ($a["priority"] == $b["priority"]) {
        return 0;
    }
    return ($a["priority"] < $b["priority"]) ? -1 : 1;
}


/*=============================================
#        测试一                : 存取变量        (*check*)\(^_^)/
=============================================*/
/*$data        = array(1,2,3,"array"=>array("a"=>1,"b"=>2,"c"=>3));
$hahah        = array("hahah","hehehe","Ohohohoho");
$cache        = new CCupCache("../data/cache");
$cache->set("data", $data);
print_r($cache->get("data"));
$cache->set("xixixi", $hahah);
print_r($cache->get("xixixi"));*/

/*=============================================
#        测试二                : 删除单个缓存                (*check*)\(^_^)/
=============================================*/
/*$cache        = new CCupCache("../data/cache");
$cache->delete("xixixi");
print_r($cache->get("xixixi"));*/

/*=============================================
#        测试三                : 配置并保存缓存配置文件        (*check*)\(^_^)/
=============================================*/
/*$hahah        = array("hahah","hehehe","Ohohohoho");
$cache        = new CCupCache("../data/cache");
$config        = array("ttl"=>10);
$cache->set("xixixi", $hahah, "c_", $config);
$cache->saveConfig();*/

/*=============================================
#        测试四                : 查询缓存配置文件                (*check*)\(^_^)/
=============================================*/
/*$cache        = new CCupCache("../data/cache");
print_r($cache->getConfig());
print_r($cache->getConfig("xixixi"));
print_r($cache->getConfig("xixixi", "a_"));
print_r($cache->getConfig("xixixi", "c_"));
print_r($cache->getConfig("xi", "", true));
print_r($cache->getConfig("ha", "", true));*/

/*=============================================
#        测试五                : 自动更新缓存                (*check*)\(^_^)/
=============================================*/
/*$cache        = new CCupCache("../data/cache");
sleep(10);
$cache->update();
$conf = $cache->getConfig();
echo "time:" .date("H:i:s", $conf["c_xixixi"]["last"]);*/
?>
[/php]

和以前一样由于村里的php标签吃斜杠,我把文件上传一下

[[i] 本帖最后由 sanders_yao 于 2007-7-6 05:33 PM 编辑 [/i]]

jonyare2007-7-8 12:40 AM
楼主强人啊

不知道练了多少年的功了,高深我不能测

浪漫谎言2007-7-9 12:45 AM
学习了~~~收下

海底的鱼2007-7-29 09:34 AM
我从来不夸人的,但是今天例外
LZ估计就是传说中的N人
赞一个,并收藏此帖,以便学习

歪歪飞扬2007-7-29 02:55 PM
强。....

yiqieqie2007-7-31 07:22 AM
跟贴  啊啊

foxbluecsky2007-8-4 09:19 AM
up!!!!!!!

fnet2007-8-4 02:13 PM
up!!!!

ccxxcc2007-8-5 01:29 PM
非一般的支持~~~~~

beluckly2007-8-6 10:30 AM
mark
收藏。。。

minglu8882007-8-6 04:24 PM
不错,不错

phpe2007-9-8 12:32 AM
NB到连我也看不懂!!~!

neteasy2007-9-11 02:41 PM
收藏!好文章!


查看完整版本: 最近写的代码 仅做交流用


Powered by Discuz! Archiver 6.1.0  © 2001-2006 Comsenz Inc.
Processed in 0.01389 second(s), 2 queries