Copilot commented on code in PR #13424:
URL: https://github.com/apache/apisix/pull/13424#discussion_r3377582057


##########
conf/config.yaml.example:
##########
@@ -519,6 +520,7 @@ plugins:                           # plugin list (sorted by 
priority)
   - data-mask                      # priority: 1500
   #- error-log-logger              # priority: 1091
   - proxy-cache                    # priority: 1085
+  - ai-cache                       # priority: 1086

Review Comment:
   This plugin list is annotated as “sorted by priority”, but `ai-cache` 
(priority 1086) is currently placed after `proxy-cache` (1085). Please reorder 
these two entries to keep the list consistent with the documented ordering.



##########
apisix/plugins/ai-cache.lua:
##########
@@ -0,0 +1,197 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core         = require("apisix.core")
+local schema_mod   = require("apisix.plugins.ai-cache.schema")
+local base         = require("apisix.plugins.ai-proxy.base")
+local key_mod      = require("apisix.plugins.ai-cache.key")
+local redis        = require("apisix.utils.redis")
+local rediscluster = require("apisix.utils.rediscluster")
+local ngx          = ngx
+local ngx_timer_at = ngx.timer.at
+
+local plugin_name = "ai-cache"
+
+-- Hardcoded in Phase 1a; Phase 1e makes these schema fields.
+local STATUS_HEADER       = "X-AI-Cache-Status"
+local MAX_CACHE_BODY_SIZE = 1048576   -- 1 MiB
+
+
+local function get_client(conf)
+    if conf.policy == "redis-cluster" then
+        local cli, err = rediscluster.new(conf, 
"plugin-ai-cache-redis-cluster-slot-lock")
+        return cli, err, "cluster"
+    end
+    local cli, err = redis.new(conf)
+    return cli, err, "single"
+end
+
+
+local function release(cli, mode, conf)
+    if mode == "cluster" then
+        -- rediscluster keeps its own pool
+        return
+    end
+    cli:set_keepalive(conf.redis_keepalive_timeout, conf.redis_keepalive_pool)

Review Comment:
   `set_keepalive` return value is ignored. If `set_keepalive` fails, the Redis 
connection may not be returned to the pool (potential connection leak) and the 
failure will be silent, making issues hard to diagnose. Please capture and log 
the error (same pattern used in existing Redis-backed plugins).



##########
docs/en/latest/plugins/ai-cache.md:
##########
@@ -0,0 +1,125 @@
+---
+title: ai-cache
+keywords:
+  - Apache APISIX
+  - API Gateway
+  - Plugin
+  - ai-cache
+  - AI
+  - LLM
+description: The ai-cache Plugin caches LLM responses and serves them on 
identical requests, reducing upstream cost and latency.
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+<head>
+  <link rel="canonical" href="https://docs.api7.ai/hub/ai-cache"; />
+</head>
+
+## Description
+
+The `ai-cache` Plugin caches responses from LLM services and serves them 
directly when an identical request arrives again, reducing upstream token cost 
and response latency. It is used together with the [`ai-proxy`](./ai-proxy.md) 
or [`ai-proxy-multi`](./ai-proxy-multi.md) Plugin.
+
+The Plugin computes a SHA-256 cache key from the request and looks it up in 
Redis before the request is forwarded upstream. On a **hit**, the cached 
response is returned directly and the upstream is never contacted. On a 
**miss**, the request is proxied normally and a successful JSON response is 
written to the cache for subsequent requests.
+
+This release implements an **exact-match cache** for the `openai-chat` 
protocol. All `openai-chat`-compatible providers (for example Azure OpenAI, 
DeepSeek, OpenRouter, Together, vLLM, Ollama) are cached transparently because 
they share the protocol.
+
+:::note
+
+The cache key is currently derived from the **client-sent** request body — 
operator-side overrides applied by `ai-proxy` (such as a `model` override) are 
not yet reflected in the key. If a route applies such overrides, flush the 
cache (or use a dedicated Redis database) before relying on cache correctness.
+
+:::

Review Comment:
   The note claims the cache key is derived from the client-sent request body 
and does not reflect `ai-proxy` instance overrides. In this PR, `ai-cache` 
explicitly hashes the *effective* (post-override) request via 
`effective_request_for_cache`, so this warning is now inaccurate and could 
mislead operators.



##########
docs/en/latest/plugins/ai-cache.md:
##########
@@ -0,0 +1,125 @@
+---
+title: ai-cache
+keywords:
+  - Apache APISIX
+  - API Gateway
+  - Plugin
+  - ai-cache
+  - AI
+  - LLM
+description: The ai-cache Plugin caches LLM responses and serves them on 
identical requests, reducing upstream cost and latency.
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+<head>
+  <link rel="canonical" href="https://docs.api7.ai/hub/ai-cache"; />
+</head>
+
+## Description
+
+The `ai-cache` Plugin caches responses from LLM services and serves them 
directly when an identical request arrives again, reducing upstream token cost 
and response latency. It is used together with the [`ai-proxy`](./ai-proxy.md) 
or [`ai-proxy-multi`](./ai-proxy-multi.md) Plugin.
+
+The Plugin computes a SHA-256 cache key from the request and looks it up in 
Redis before the request is forwarded upstream. On a **hit**, the cached 
response is returned directly and the upstream is never contacted. On a 
**miss**, the request is proxied normally and a successful JSON response is 
written to the cache for subsequent requests.
+
+This release implements an **exact-match cache** for the `openai-chat` 
protocol. All `openai-chat`-compatible providers (for example Azure OpenAI, 
DeepSeek, OpenRouter, Together, vLLM, Ollama) are cached transparently because 
they share the protocol.
+
+:::note
+
+The cache key is currently derived from the **client-sent** request body — 
operator-side overrides applied by `ai-proxy` (such as a `model` override) are 
not yet reflected in the key. If a route applies such overrides, flush the 
cache (or use a dedicated Redis database) before relying on cache correctness.
+
+:::
+
+## Attributes
+
+| Name                       | Type            | Required                      
    | Default | Valid values         | Description                              
                                                            |
+|----------------------------|-----------------|-----------------------------------|---------|----------------------|------------------------------------------------------------------------------------------------------|
+| exact.ttl                  | integer         | False                         
    | 3600    | 1 to 2592000         | Time-to-live in seconds for a cached 
response.                                                       |
+| policy                     | string          | True                          
    | redis   | redis, redis-cluster | Backend used to store cached responses.  
                                                            |
+| redis_host                 | string          | True when `policy` is `redis` 
    |         |                      | Address of the Redis node.               
                                                            |
+| redis_port                 | integer         | False                         
    | 6379    | greater than 0       | Port of the Redis node.                  
                                                            |
+| redis_password             | string          | False                         
    |         |                      | Password for Redis authentication.       
                                                            |
+| redis_database             | integer         | False                         
    | 0       | greater than or equal to 0 | Redis database to use when 
`policy` is `redis`.                                                |
+| redis_timeout              | integer         | False                         
    | 1000    | greater than 0       | Redis connection/read/write timeout in 
milliseconds.                                                 |
+| redis_cluster_nodes        | array[string]   | True when `policy` is 
`redis-cluster` |     |                      | List of Redis cluster node 
addresses, used when `policy` is `redis-cluster`.                         |
+| redis_cluster_name         | string          | True when `policy` is 
`redis-cluster` |     |                      | Name of the Redis cluster, used 
when `policy` is `redis-cluster`.                                    |
+| redis_cluster_ssl          | boolean         | False                         
    | false   |                      | If true, use SSL to connect to the Redis 
cluster.                                                    |
+| redis_cluster_ssl_verify   | boolean         | False                         
    | false   |                      | If true, verify the Redis cluster SSL 
certificate.                                                   |

Review Comment:
   The plugin schema (via `apisix.utils.redis-schema`) supports additional 
Redis connection fields (for example `redis_username`, `redis_ssl`, 
`redis_ssl_verify`, and keepalive settings), and `ai-cache` also uses 
`redis_keepalive_timeout/pool` at runtime. The Attributes table should document 
these options for parity with other Redis-backed plugin docs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to