| 139 | } |
| 140 | |
| 141 | void handlePut(const std::string& key) { |
| 142 | // Flatten body |
| 143 | std::unique_ptr<folly::IOBuf> buf = bodyQueue_.move(); |
| 144 | auto body = buf->coalesce(); |
| 145 | if (body.size() == 0) { |
| 146 | ResponseBuilder(downstream_) |
| 147 | .status(400, "Bad Request") |
| 148 | .body("Empty body\n") |
| 149 | .sendWithEOM(); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | WriteHandle wh = ctx_->cache->allocate(ctx_->poolId, key, body.size()); |
| 154 | if (!wh) { |
| 155 | // Allocation failed (e.g., item too big); try eviction via remove + retry |
| 156 | // could be added. |
| 157 | ResponseBuilder(downstream_) |
| 158 | .status(507, "Insufficient Storage") |
| 159 | .body("Allocation failed\n") |
| 160 | .sendWithEOM(); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | std::memcpy(wh->getMemory(), body.data(), body.size()); |
| 165 | ctx_->cache->insertOrReplace(std::move(wh)); |
| 166 | |
| 167 | ResponseBuilder(downstream_) |
| 168 | .status(201, "Created") |
| 169 | .body("OK\n") |
| 170 | .sendWithEOM(); |
| 171 | } |
| 172 | |
| 173 | void handleDelete(const std::string& key) { |
| 174 | auto res = ctx_->cache->remove(key); |