yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Allow configuration of the cache used by the url manager #16721

Closed hofrob closed 6 years ago

hofrob commented 6 years ago

What steps will reproduce the problem?

What is the expected result?

Additional info

https://github.com/yiisoft/yii2/blob/master/framework/web/UrlManager.php#L278

Q A
Yii version 2.0.15.1
PHP version 7.2.9
Operating system docker/alpine
paweljankowiak06 commented 6 years ago

Same could be said about rbac DbManager. There is no possibility to clear cache only for texts categories.

hofrob commented 6 years ago

Now that I think about it.

In yii\rbac\DbManager you could just use the same cache with a different config:

https://github.com/yiisoft/yii2/blob/master/framework/rbac/DbManager.php#L64

The cache property accepts an array as config for the cache.

And in yii\web\UrlManager you can use a dedicated cache application component. Still confusing that there are two different approaches to the same problem.

rob006 commented 6 years ago

And in yii\web\UrlManager you can use a dedicated cache application component. Still confusing that there are two different approaches to the same problem.

This actually should be fixed - Instance::ensure() should be consistently used in such cases.

We could also make $cacheKey public, so cache could be revalidated by changing this value. But this will break BC, so probably not an option in 2.0.

paweljankowiak06 commented 6 years ago

There are few places where such behavior is implemented. Maybe some common trait would be a good solution.

mjvanmiddelaar commented 6 years ago

I'm not sure if it is related to this issue: i would like to set a cache dependency for the UrlManager cache in https://github.com/yiisoft/yii2/blob/master/framework/web/UrlManager.php#L278, both duration and dependency are always the default null - or should i open a new issue?

In my scenario I have an url rule like this:

<?php

namespace app\rules;

class SlugUrlRule extends \yii\web\UrlRule
{

    public $modelClass;

    public $attribute = 'slug';

    public $paramName = 'slug';

    public function init()
    {
        $slugs = array_filter($this->modelClass::find()->select($this->attribute)->column());
        $this->pattern = '<'.$this->paramName.':(' . implode('|', $slugs) . ')>';
        parent::init();
    }
}
samdark commented 6 years ago

About original issue. You can specify separate cache instance there:

So I don't see a problem with flushing this separate instance.

@mahkali what for?

mjvanmiddelaar commented 6 years ago

@samdark flushing the cache on change would indeed be more efficient as we don't require to check a dependency on every request.