缓存是提升 Web 应用性能简便有效的方式。
通过将相对静态的数据存储到缓存并在收到请求时取回缓存,
应用程序便节省了每次重新生成这些数据所需的时间。
缓存可以应用在 Web 应用程序的任何层级任何位置。
在服务器端,在较的低层面,缓存可能用于存储基础数据,例如从数据库中取出的最新文章列表;
在较高的层面,缓存可能用于存储一段或整个 Web 页面,
例如最新文章的渲染结果。在客户端,HTTP 缓存可能用于
将最近访问的页面内容存储到浏览器缓存中。
Caching is a cheap and effective way to improve the performance of a Web application. By storing relatively
static data in cache and serving it from cache when requested, the application saves the time that would be
required to generate the data from scratch every time.
Caching can occur at different levels and places in a Web application. On the server-side, at the lower level,
cache may be used to store basic data, such as a list of most recent article information fetched from database;
and at the higher level, cache may be used to store fragments or whole of Web pages, such as the rendering result
of the most recent articles. On the client-side, HTTP caching may be used to keep most recently visited page content in
the browser cache.
ShopSuite采用Zero\Cache类提供缓存功能支持。
内置支持的缓存类型包括file、memcache、redis等等
设置
全局的缓存配置直接修改配置目录下面的/shop/configs/cache.ini.php
文件。
你可以事先定义好所有的缓存类型及配置参数,然后在使用的时候可以随时切换。默认使用的是文件缓存类型,你可以添加redis缓存支持,例如:
<?php
//default 默认,必须有, 其它配置,如果没有设置,在调用default
$config_cache['memcache']['default'] = array(
array(
'127.0.0.1' => '11211'
),
);
$config_cache['memcache']['base'] = array(
array(
'127.0.0.1' => '11211'
),
);
$config_cache['memcache']['html'] = array(
array(
'127.0.0.1' => '11211'
),
);
$config_cache['memcache']['wechat'] = array(
array(
'127.0.0.1' => '11211'
),
);
$config_cache['memcache']['verify_code'] = array(
array(
'127.0.0.1' => '11211'
),
);
//redis 配置
$config_cache['redis']['default'] = array(
array(
'127.0.0.1' => '6379'
),
);
$config_cache['redis']['base'] = array(
array(
'127.0.0.1' => '6379'
),
);
$config_cache['redis']['html'] = array(
array(
'127.0.0.1' => '6379'
),
);
$config_cache['redis']['wechat'] = array(
array(
'127.0.0.1' => '6379'
),
);
$config_cache['redis']['verify_code'] = array(
array(
'127.0.0.1' => '6379'
),
);
$config_cache['redis']['redis_data'] = array(
array(
'127.0.0.1' => '6379'
),
);
//设置cache 参数, 有些文件html缓存固定,有些可以来设置决定
//cacheType 0(11:apc 12:EAccelerator 13:xdebug):动态获取 1:file 2:memcache 3:redis
//lifeTime null, 永久生效, memcache null|0 效果一样, file 必须为null
//$cache_type 可以动态替换如下设置
$data_path = ROOT_PATH . DS . (APP_DIR_NAME == 'shop_admin' ? 'shop' : APP_DIR_NAME) . DS . 'data' . DS . DATA_ID;
$data_path = ROOT_PATH . DS . ('shop') . DS . 'data' . DS . DATA_ID;
$config_cache['default'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/default_cache/',
'memoryCaching' => false,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 2,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['base'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/base_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['product'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/product_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['store'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/store_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['user'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/user_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['order'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/order_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['consume'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/consume_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['activity'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/activity_cache/',
'memoryCaching' => true,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['html'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/html_cache/',
'memoryCaching' => false,
'automaticSerialization' => false,
'hashedDirectoryLevel' => 2,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['redis_data'] = array(
'cacheType' => 3,
'cacheDir' => $data_path . '/cache/redis_data_cache/',
'memoryCaching' => false,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 2,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['wechat'] = array(
'cacheType' => 2,
'cacheDir' => $data_path . '/cache/wechat_cache/',
'memoryCaching' => false,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_cache['verify_code'] = array(
'cacheType' => 1,
'cacheDir' => $data_path . '/cache/verify_code_cache/',
'memoryCaching' => false,
'automaticSerialization' => true,
'hashedDirectoryLevel' => 1,
'lifeTime' => 3600,
'dbName' => 1,
'auth' => 'foobared'
);
$config_row['config_cache'] = $config_cache;
return $config_row;