codeliter/cacheadapter

用于使用 Nette 缓存组件的适配器

安装: 29

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:项目

1.3.3 2019-03-05 02:11 UTC

This package is auto-updated.

Last update: 2024-09-05 18:51:37 UTC


README

这个库本身不实现任何功能,它是为了更好地与 Nettecache 库进行通信而编写的。它只是一个适配器,用于简化使用 memcached 或文件存储的缓存。

要求

  • Php >= 5.4

  • Composer

  • Php Memcached 扩展(可选)

安装

  • 打开您的默认终端程序

  • 将工作目录更改为您希望使用此库的项目目录。

    您可以在 mac/Linux 上运行以下命令。

    cd /var/www/html/<project-directory>
    

    是您的项目所在位置

  • 运行命令导入库

    composer require codeliter/cacheadapter
    
  • 这样就完成了

用法

使用此库可以像这样简单

  • 初始化缓存库并设置缓存保存的绝对或相对路径

    -如果您的服务器已加载 Memcached,则默认使用 memcached。

      $cache = new Codeliter\CacheAdapter\Cache('./data/cache');
    
  • 您可以将任何内容保存到缓存中,如下所示

      $cache_name = 'test_cache';
      $data = 'This is the content of the test_case';
      $cache->save($cache_name, $data, '4 Hours');
    
  • 您可以从缓存中获取任何内容

      $cache_name = 'test_cache';
      $data = $cache->get($cache_name);
    
  • 您可以从缓存中删除

      $cache_name = 'test_cache';
      $cache->delete($cache_name);
    
  • 典型用法

    • 进行 API 或数据库请求并将结果存储在缓存中,直到数据不再变化为止

        $cache_name = 'google_home_page';
        
        try {
            // Check if the data already exists
            $data = $cache->get($cache_name);
        }
        catch (Throwable $t) {
            // If data does not exist
            // Make the API or Database request
            $request = file_get_contents('https://google.com');
            // Store the data to cache for sometime for future requests
            $cache->save($cache_name, $request, '24 Hours');
        }
      

致谢