【php】缓存类的编写:支持文件缓存和memcache缓存

<?php
//与上一篇文章:使用mysqli完成的一个数据库操作类     搭配使用
abstract class Abstract_Cache {
	protected $cache_time;
	public function set_cache_time($cache_time) {
		$this->cache_time    = $cache_time;//设置缓存时间,可写在公共的配置文件中
	}
	abstract function &get($key);
	abstract function set($key, $data);
	abstract function add($key, $data);
	abstract function del($key);
	abstract function get_key($key);
	abstract function flush();
}

class cache_file extends Abstract_Cache {
	private $cache_dir;
	public function set_cache_dir($cache_dir = './cache/') {
		$this->cache_dir    = $cache_dir;
	}
	public function mkpath($dir) {
                //层级的创建目录
                return is_dir($dir) or ($this->mkpath(dirname($dir)) and (mkdir($dir, 0777) and chmod($dir,0777)));
	}
	public function &get($key, $cache_time = '') {
		if(empty ($cache_time)) {
			$cache_time	= $this->cache_time;
		} else {
			$cache_time	= intval($cache_time);
		}
		$cache_encode_key  = $this->get_key($key);
                //缓存文件的名称
                $filename = $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/'.$cache_encode_key.".cache.php";
		//缓存文件是否是否存在和过期
                if(!is_file($filename) || time() - filemtime($filename) > $cache_time) {
			return false;
		} else {
			return unserialize(file_get_contents($filename));//文件中缓存的数据为序列化格式的数据,所以取出时需要将其反序列化
		}
	}
	public function set($key,$data) {
		$cache_encode_key  = $this->get_key($key);
		$cache_dir	= $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/';
		$filename	= $cache_dir.$cache_encode_key.".cache.php";
		if( $this->mkpath($cache_dir) ) {
			$out    = serialize($data);
			file_put_contents($filename, $out);
		}
	}
	public function add($key, $data) {
		$cache_encode_key	= $this->get_key($key);
		$cache_dir	= $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/';
		$filename	= $cache_dir.$cache_encode_key.".cache.php";
		if(is_file($filename)) {
			return false;
		} else {
			$this->set($key, $data);
		}
	}
	public function del($key) {
		$cache_encode_key	= $this->get_key($key);
		$cache_dir	= $this->cache_dir.'/'.substr($cache_encode_key,0,2).'/'.substr($cache_encode_key,2,2).'/';
		$filename	= $cache_dir.$cache_encode_key.".cache.php";
		if(is_file($filename)) {
			return false;
		} else {
			@unlink($filename);
		}
	}
	public function flush() {
		$this->remove_dir($this->cache_dir,false);
	}
	public function get_key($key){
		Return md5(CACHE_KEY.$key);
	}
	private function remove_file($dir) {
		if (is_dir($dir)) {
			$dh = opendir($dir);
			while (false !== ($file = readdir($dh))) {
				if($file!="." && $file!="..") {
					$fullpath=$dir."/".$file;
					if(!is_dir($fullpath)) {
						@unlink($fullpath);
					} else {
						$this->remove_dir($fullpath);
					}
				}
			}
			closedir($dh);
		}
	}
	private function remove_dir($dir, $self = true) {
		$this->remove_file($dir);
		if ($self == true && is_dir($dir)) {
			rmdir($dir);
		}
	}
}

class cache_memcache extends Abstract_Cache {
    var $_memcache = null;
    /**
     *    连接到缓存服务器
     *
     *    @author    
     *    @param     array $options
     *    @return    bool
     */
    function __construct($memcache_conf)
    {
        $this->_memcache = new Memcache;
        return $this->_memcache->connect($memcache_conf['host'], $memcache_conf['port']);
    }

    /**
     *    写入缓存
     *
     *    @author    
     *    @param    none
     *    @return    void
     */
    function set($key, $value, $ttl = null)
    {
		$cache_encode_key = $this->get_key($key);
        return $this->_memcache->set($cache_encode_key, $value, $ttl);
    }

    /**
     *    获取缓存
     *
     *    @author    
     *    @param     string $key
     *    @return    mixed
     */
    function &get($key)
    {
		$cache_encode_key = $this->get_key($key);
        return $this->_memcache->get($cache_encode_key);
    }

    /**
     *    写入缓存
     *
     *    @author    
     *    @param    none
     *    @return    void
     */
    function add($key, $value, $ttl = null)
    {
		$cache_encode_key = $this->get_key($key);
        return $this->_memcache->add($cache_encode_key, $value, $ttl);
    }

    /**
     *    清空缓存
     *
     *    @author    
     *    @return    bool
     */
    function flush()
    {
        return $this->_memcache->flush();
    }

    function del($key)
    {
		$cache_encode_key = $this->get_key($key);
        return $this->_memcache->delete($key);
    }

	public function get_key($key)
	{
		$cache_encode_key = $this->get_key($key);
		Return md5(CACHE_KEY.$key);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程中,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证码等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据。数据解析:将请求下来的数据进行过滤,提取我们想要的数据。数据存储:将提取到的数据存储到硬盘或者内存中。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程中可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证码识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架、Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值