Developed with love by KnpLabs Hire us for your project!
16

cache

by phpsed

Symfony response cache

phpsed/cache

Latest Stable Version
Latest Unstable Version
License
Total Downloads

Someone made Medium article about this repository, check it out here!

Phpsed cache is annotation based controller response cache for Symfony framework.
It generates route specific key from GET and POST parameters and saves it in provided cache clients.

Currently supported clients are Predis and DoctrineOrm.


phpsed_cache:
    enabled: true|FALSE
    providers:
        - snc_redis.session
        - doctrine.orm.default_entity_manager

enabled parameter by default is set to false, which means that @Cache annotation won't work without setting it to true.

providers must be either Predis or Doctrine Orm clients.

In the example above, two providers are provided:

Predis client from snc/redis-bundle - snc_redis.session and Doctrine entity manager - doctrine.orm.default_entity_manager.

If enabled parameters is set as true, at least one valid provider must be provided.

Phpsed\Cache uses Symfony\Cache ChainAdapter
text
When an item is not found in the first adapter but is found in the next ones, this adapter ensures that the fetched item is saved to all the adapters where it was previously missing.


use Phpsed\Cache\Annotation\Cache;
use Symfony\Component\Routing\Annotation\Route;

@Route("/{id}", name="default", defaults = {"id" = 0})
@Cache(
    expires = 3600,
    attributes = {"id"}
)

By default @Cache annotation doesn't need any parameters.

Without attributes parameter, cache key for route is made from all of GET and POST parameters.

Without expires parameter, cache is saved for unlimited ttl which means that it will be valid until deleted.

If the same parameter exist in GET and POST, value of GET parameter will be used.

@Cache annotation takes two optional parameters: expires and attributes.

expires sets maximum ttl for given cache.

In the example above cache will be saved for 3600 seconds and after 3600 seconds it will be invalidated.

If the attributes parameter is set and given attribute(s) exist in GET or POST parameters,
only given parameters will be used in cache key.
Only valid attributes will be used in creation of key. If none of given attributes exist, key will be made without any parameters.


PS-CACHE: PS-CACHE-DISABLE

In order to invalidate and delete the cache for endpoint, you must call this endpoint with specific header.
For this you need to set PS-CACHE header with PS-CACHE-DISABLE value.

MIT License

Copyright (c) 2018 phpsed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • fix strict typing bug
    By web-flow, 19 days ago
  • Update README.md
    By web-flow, 20 days ago
  • Fix required packages
    By web-flow, 21 days ago
  • Update README.md
    By web-flow, 21 days ago
  • composer.json typo fixed
    By phpsed, 21 days ago
  • Update README.md
    By web-flow, 21 days ago
  • Update README.md
    By web-flow, 21 days ago
  • Phpsed\Cache created
    By phpsed, 21 days ago
  • composer.json update
    By phpsed, 21 days ago
  • README.md update
    By phpsed, 21 days ago
  • Initial commit
    By phpsed, 22 days ago
  • Initial commit
    By web-flow, 23 days ago