Home Backend Development PHP Tutorial 简单的php缓存类分享 php缓存机制_PHP

简单的php缓存类分享 php缓存机制_PHP

Jun 01, 2016 am 11:56 AM
php caching technology php caching mechanism

复制代码 代码如下:
class Cache
{
 private $dir = "data/cache/";//定义缓存目录 
 private $key='c_a_sss'; // 文件名md5加密密钥

 function set_dir($dirpath)
 {
  $this->dir=$dirpath;
  $this->make_dir($this->dir);
 }
 function read($key,$minutes=1)
 {
  $filename=$this->get_filename($key);
  if($datas = @file_get_contents($filename))
  {
    $datas = unserialize($datas);
    if(time() - $datas['time']     {
     return $datas['data'];
    }
  }
  return false;
 }

 function write($key,$data)
 {  
  $filename=$this->get_filename($key);
  if($handle = fopen($filename,'w+'))
  {
   $datas = array('data'=>$data,'time'=>time());
   flock($handle,LOCK_EX);
   $rs = fputs($handle,serialize($datas));
   flock($handle,LOCK_UN);
   fclose($handle);
   if($rs!==false){return true;  }
  }
  return false;
 }
 function clear_all()
 {
  $dir=$this->dir;
  $this->del_file($dir); 
 }

  private function get_filename($key)
 {
  return $this->dir.$key.'_'.md5($key.$this->key);
 }
 private function make_dir($path)
 {
  if (! file_exists ( $path ))
  {
   $this->make_dir ( dirname ( $path ) );
   if (! mkdir ( $path, 0777 ))
   die ( '无法创建缓存文件夹' . $path );
  }
 }
 private function del_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->del_file($fullpath);
     }
    }
   }
   closedir($dh);
  }
 }
}

$cache = new cache();
  $cache->set_dir('data/cache_dir/');
  $data=$cache->read('sys',1);
  if(empty($data))
  {
   $data=array('aa'=>1111,'bb'=>2222,'date'=>date('Y-m-d H:i:s'));
   $cache->write('sys',$data); 
  }
  print_r($data);

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dynamic data caching through caching technology in PHP? How to implement dynamic data caching through caching technology in PHP? Jun 21, 2023 am 08:10 AM

With the continuous development of Internet applications, optimizing website performance has become one of the necessary tasks for website development. Among them, the use of caching technology is an important optimization method. In PHP development, caching technology can improve the performance and response speed of the website, effectively avoid operations such as repeated calculations and database queries, and thereby achieve caching of dynamic data. This article will introduce how to use caching technology to implement dynamic data caching in PHP. The concept of caching Caching is a technology used to improve application performance. In website development, caching is caching service

How to Optimize Website Performance and Loading Speed ​​with PHP How to Optimize Website Performance and Loading Speed ​​with PHP Sep 12, 2023 am 10:13 AM

How to use PHP to optimize website performance and loading speed With the rapid development of the Internet, website performance and loading speed have attracted more and more attention. As a widely used server-side scripting language, PHP plays an important role in optimizing website performance and loading speed. This article will introduce some tips and methods for using PHP to improve the performance and loading speed of your website. Using a caching mechanism Caching is an effective way to improve website performance. PHP provides a variety of caching mechanisms, such as file caching, memory caching and data caching.

Analysis of the effectiveness of caching technology in PHP for static resource files Analysis of the effectiveness of caching technology in PHP for static resource files Jun 19, 2023 pm 10:49 PM

PHP is a server-side programming language widely used in web development. In the process of developing a website, the loading speed of static resource files (including css, js, pictures, etc.) directly affects the user experience of the website. Therefore, how to improve the loading speed of static resource files has become one of the issues that developers need to think about. One solution is to use caching technology in PHP. In PHP, the caching of static resource files is mainly divided into two types: browser cache and server cache. Browser caching relies on the browser's local caching mechanism to reduce

Understanding PHP caching mechanisms: exploring different implementations Understanding PHP caching mechanisms: exploring different implementations Jan 23, 2024 am 09:53 AM

Explore the PHP caching mechanism: Understand different implementation methods and require specific code examples. The caching mechanism is a very important part of web development and can greatly improve the performance and response speed of the website. As a popular server-side language, PHP also provides a variety of caching mechanisms to optimize performance. This article will explore PHP's caching mechanism, introduce different implementation methods, and provide specific code examples. File Cache File cache is one of the simplest and most common PHP caching methods. Its principle is very simple

Caching technology in PHP and its implementation method Caching technology in PHP and its implementation method Jun 23, 2023 am 11:31 AM

As the complexity of modern web applications continues to increase, performance issues have become a major challenge for developers. One of the common performance bottlenecks is frequent access to the database or file system, which can cause serious performance issues. Caching technology is one way to solve these problems. This article will introduce the basic knowledge and implementation methods of using cache in PHP. We will discuss some popular PHP caching techniques and how to integrate them into our applications. What is cache? Caching is a way for an application to

Encapsulated caching strategies and techniques in PHP Encapsulated caching strategies and techniques in PHP Oct 12, 2023 am 10:51 AM

Encapsulated caching strategies and technologies in PHP Caching is one of the important means to improve application performance. In PHP development, reasonable use of cache can reduce the number of database queries and increase data reading speed, thus improving the application response speed and user experience. The encapsulated caching strategy refers to encapsulating cache operations into common code blocks to facilitate reuse in multiple places and facilitate unified management and configuration. Below we will introduce several common encapsulation caching strategies and technologies, and give specific code examples. File cache File cache is the most

Summary of PHP caching technology methods and common problems Summary of PHP caching technology methods and common problems Jun 08, 2023 pm 04:40 PM

Summary of PHP caching technology methods and common problems As the number of visits to the website continues to increase, caching technology is particularly important in order to improve the response speed and performance of the website. In PHP development, caching technology also plays a vital role. This article will introduce methods of PHP caching technology and solutions to common problems. 1. PHP caching technology methods 1. File caching File caching refers to caching data in files, and you can directly read the files when you need to use them. The specific implementation method is to serialize the data and store it in the file. When reading,

Detailed explanation of PHP caching mechanism: in-depth exploration of its working principle and practical application Detailed explanation of PHP caching mechanism: in-depth exploration of its working principle and practical application Jan 23, 2024 am 09:13 AM

Full analysis of PHP caching mechanism: in-depth understanding of its principles and applications Introduction: In developing web applications, caching is an important technical means that can significantly improve application performance and user experience. As a commonly used server-side programming language, PHP also provides a rich caching mechanism for developers to use. This article will delve into the principles and applications of PHP caching mechanism and give specific code examples. 1. Principles of Caching Before introducing the PHP caching mechanism, we need to understand the basic principles of caching. Caching is a way of storing data

See all articles