CacheFile::set PHP Method

set() public method

写入缓存
public set ( string $name, mixed $value, integer $expire = null ) : boolen
$name string 缓存变量名
$value mixed 存储数据
$expire integer 有效时间 0为永久
return boolen
    public function set($name, $value, $expire = null)
    {
        N('cache_write', 1);
        if (is_null($expire)) {
            $expire = $this->options['expire'];
        }
        $filename = $this->filename($name);
        $data = serialize($value);
        if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
            //数据压缩
            $data = gzcompress($data, 3);
        }
        if (C('DATA_CACHE_CHECK')) {
            //开启数据校验
            $check = md5($data);
        } else {
            $check = '';
        }
        $data = "<?php\n//" . sprintf('%012d', $expire) . $check . $data . "\n?>";
        $result = file_put_contents($filename, $data);
        if ($result) {
            if ($this->options['length'] > 0) {
                // 记录缓存队列
                $this->queue($name);
            }
            clearstatcache();
            return true;
        } else {
            return false;
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Tests CacheFile->set()
  */
 public function testSetException()
 {
     @rmdir("/tmp/shindig/te");
     $this->assertTrue(touch("/tmp/shindig/te"));
     $this->setExpectedException("CacheException");
     try {
         $this->CacheFile->set("test", "testing");
     } catch (Exception $e) {
         $this->assertTrue(unlink("/tmp/shindig/te"));
         throw $e;
     }
 }
All Usage Examples Of CacheFile::set