bigpaulie/cachebundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bigpaulie/cachebundle

Composer 安装命令:

composer require bigpaulie/cachebundle

包简介

This bundle provides some extra functionality for multi-domain and ORM caching

README 文档

README

This bundle provides some extra functionality to the cache layer by exposing a service and a trait in order to make it easier for you to cache your queries.

At the moment this bundle works with Symfony 2.8.x PHP 5.6, PHP 7 and MySQL/MariaDB

Installation

The preferred way to install the bundle is via composer

composer require bigpaulie/symfony-cachebundle "dev-master" --prefer-dist

After requiring the package you should add the bundle to your bundle array in the AppKernel.php

$bundles = array(
        ...
        new bigpaulie\CacheBundle\BigpaulieCacheBundle(),
);

Import the services.yml in your config.yml at the top of the file

- { resource: "@BigpaulieCacheBundle/Resources/config/services.yml"}

Add a new parameter in parameters.yml

memcached_servers:
        - { host: 127.0.0.1, port: 11211 }

Enable/disable caching for specific environments

Place the following in your config_*.yml file

    bigpaulie_cache:
        enable: true|false

Tell doctrine to use a given cache driver

doctrine:
    orm:
        metadata_cache_driver:
            type: service
            id: doctrine.cache.driver.memcached
        query_cache_driver:
            type: service
            id: doctrine.cache.driver.memcached
        result_cache_driver:
            type: service
            id: doctrine.cache.driver.memcached

Query Caching

The bundle follows the official API as described in the documentation at Doctrine Project

DQL Caching

// $qb instanceof QueryBuilder

$qb->select('u')
   ->from('User', 'u')
   ->where('u.id = ?1')
   ->orderBy('u.name', 'ASC')
   ->setParameter(1, 100);
   
$query = $qb->getQuery();
$query->setQueryCaching(true);

$result = $query->getResult();

The setQueryCaching() method supports two additional parameters: lifetime and key.

$query->setQueryCaching(true, 3600, 'my_unique_key');

Entity Caching

Symfony Framework doesn't support entity caching out of the box, here enters the Cacheable trait. Use the Cacheable trait in the repositories you want to cache.

use bigpaulie\CacheBundle\Doctrine\Support\Cacheable;
Overridden methods:

find()

 find($id, $lifetime = null, \Closure $callable = null)

If you specify a lifetime for your cache than the result will be cached.

Additionally there is a third parameter to which you can pass an Closure which will act like a default return in case the query doesn't return any result.

findOneBy()

findOneBy(array $criteria, array $orderBy = null, $lifetime = null, \Closure $callable = null)

If you specify a lifetime for your cache than the result will be cached.

Additionally you can also pass a Closure as the forth parameter which will act as a default return if the query doesn't return any results.

findAll()

findAll($lifetime = null, \Closure $callable = null)

If you specify a lifetime for your cache than the result will be cached.

Additionally you can also pass a Closure as the second parameter which will act as a default return if the query doesn't return any results.

Example Usage
use bigpaulie\CacheBundle\Doctrine\Support\Cacheable;
use Doctrine\ORM\EntityRepository;

/**
 * SomeRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SomeRepository extends EntityRepository
{
    use Cacheable;
}
// No caching
$this->getDoctrine()->getRepository('SomeBundle:SomeEntity')->find(1);

// Caching for 3600 seconds
$this->getDoctrine()->getRepository('SomeBundle:SomeEntity')->find(1, 3600);

// Caching for 3600 seconds and passing a Closure
$this->getDoctrine()->getRepository('SomeBundle:SomeEntity')->find(1, 3600, function () {
    return new NullObject();
});

Contribution

Feel free to contribute to this project, together we can make it a better project.

Fork, code, submit a pull request or open a issue.

统计信息

  • 总下载量: 504
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固