> For the complete documentation index, see [llms.txt](https://doc.kaliphp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.kaliphp.com/cache.md).

# 缓存

框架提供了可基于`file`、`memcache`、`redis`三种缓存类型，通过修改`config/config.php`进行设置，`cache_type`参数用于切换，完整示例如下：

```
// config/config.php
'cache' => array(
    'enable'     => true,
    'prefix'     => 'mc_df_',
    'cache_type' => 'redis',      // 缓存类型 file、memcache、redis
    'cache_time' => 7200,
    'cache_name' => 'cfc_data',
    'serialize' => true,          // 开启redis自动序列化存储
    'memcache' => array(
        'timeout' => 1,
        'servers' => array(
            array( 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 1 ),
        )
    ),
    // redis目前只支持单台服务器
    'redis' => array(
        'timeout' => 30,
        'server' => array( 'host' => '127.0.0.1', 'port' => 6379, 'pass' => 'foobared')
    ),

    'session' => array(
        'type'   => 'cache',      // session 缓存类型 default、cache、mysql
        'expire' => 1440,         // session 回收时间 默认24分钟:1440、一天:86400
    )
)
```

Cache支持`set`、`get`、`del`、`ttl`、`inc`、`dec`等简单操作方法

```
// 添加缓存
cache::set('name', 'kali');

// 获取缓存
$name = cache::get('name');

// 删除缓存
$name = cache::del('name');

// 添加缓存并设置一个小时有效期
cache::set('email', 'kaliphp@gmail.com', 3600);

// 自增缓存，默认增加1
$num = cache::inc('num');

// 自减缓存，默认减少1
$num = cache::dec('num');
```

Cache还支持在知道使用何种缓存的时候使用其方法，比如driver是Redis，那么我们可以直接使用Redis的其他方法

```
// 添加队列
cache::lpush('new-list', '1111');

// 弹出队列
cache::rpop('new-list');
```

### Redis

当我们缓存没有使用Redis的情况下，但是又需要用到Redis的队列功能的时候，我们可以使用 cls\_redis.php 类进行操作，配置请看上面的Cache中的Redis部分

```
// 添加队列
cls_redis::instance()->lpush('new-list', '1111');

// 弹出队列
cls_redis::instance()->rpop('new-list');
```

Redis类不支持主从操作，但是支持多实例，所以在使用的时候可以通过不同配置创建多个实例来操作Redis

```
// 默认实例，调用默认配置
$redis1 = cls_redis::instance();

// 添加一个实例，手动配置
$config = [
    'host' => '192.168.0.2', 
    'port' => 6379,
    'pass' => 'foobared'
];
$redis2 = cls_redis::instance('redis2', $config);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.kaliphp.com/cache.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
