Scale zf2 redis Zend framework 2 redis extend-key regular

Source: Internet
Author: User
Tags zend framework

As the business progresses, You Need To directly operate redis data!

You need to obtain the key with the specified prefix, just like operating mysql. You need to obtain the specified record segment!

Zf2 strongly supports redis cache, but when I used zf2 redis cache factory mode, I found that zf2 does not support redis keys!

// Thanks to the blog: http://my.oschina.net/cart/

The following describes how to expand zf2 redis:

\module\Application\src\Application\Service\RedisExtendService.php<?phpnamespace Application\Service;use Zend\Cache\Storage\Adapter\Redis;class RedisExtendService extends Redis{    public function __construct($options = null)    {        parent::__construct($options);    }    /**     * support regular     *     * @param sting $keys                 */    public function getKeys($keys)    {        return $this->getRedisResource()->keys($keys);    }}
OK. expansion is complete!

In indexaction, we directly use our extended zf2 redis. As long as you are willing to scale and don't want to be limited to zf2, you can use many original redis methods:

<?phpnamespace Application\Controller;use Zend\Mvc\Controller\AbstractActionController;use Zend\View\Model\ViewModel;class IndexController extends AbstractActionController{    private static $redis;        private function redis(){        if(!self::$redis){$config = $this->getServiceLocator()->get(‘config‘);            self::$redis = new \Application\Service\RedisExtendService($config[‘Redis‘]);        }        return self::$redis;    }    public function indexAction(){        var_dump($this->redis()->getKeys(‘Member_*‘));var_dump($this->redis()->getKeys(‘Item_*‘));var_dump($this->redis()->getKeys(‘Product_*‘));    }}


With this article, we can draw a lot from the opposite: a large number of inheritance extensions zf2!

And your expansion is efficient and portable!



Scale zf2 redis Zend framework 2 redis extend-key regular

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.