Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different TTL with GreedyCacheStrategy and RequestMatcher #147

Open
vaclavgreif opened this issue Aug 27, 2021 · 2 comments
Open

Different TTL with GreedyCacheStrategy and RequestMatcher #147

vaclavgreif opened this issue Aug 27, 2021 · 2 comments

Comments

@vaclavgreif
Copy link

vaclavgreif commented Aug 27, 2021

I'm defining a DelegatingCacheStrategy, and I'm using a simple config to define which endpoint should be cached:

$cache_config = [
	'get' => [
		'agreements'
	],
];
$cache        = new Cache( new FilesystemAdapter('testCache',0, __DIR__), $cache_config );
$strategy = new DelegatingCacheStrategy( $defaultStrategy = new NullCacheStrategy() );
			$strategy->registerRequestMatcher( new CacheRequestMatcher($this->cache->getConfig()), new GreedyCacheStrategy( new Psr6CacheStorage( $this->cache->getCacheItemPool() ), 1600 ) );
			$stack->push( new CacheMiddleware( $strategy ) );

and the matcher method looks like this:

/**
	 * @inheritDoc
	 */
	public function matches( RequestInterface $request ) {
		$request_type = strtolower( $request->getMethod() );
		if (empty($this->config[$request_type])) {
			return false;
		}

		foreach ( $this->config[$request_type] as $endpoint ) {
			if (strpos($request->getUri()->getPath(), $endpoint) !== false) {
				return true;
			}
		}

		return false;
	}

This works great, but I'd need to be able to define different TTLs for different endpoints. Is there a way to set that somehow?

@marcus-at-localhost
Copy link

@vaclavgreif did you figure that out? I need that too :/

@vaclavgreif
Copy link
Author

Hi Marcus, in the end I did something like this:

$cache_rules  = [
	new Rule( Rule::REQUEST_TYPE_GET, 'agreements', 400 ),
];
$cache        = new Cache(
	new FilesystemAdapter( 'testCache', 0, __DIR__ ),
	$cache_rules
);

$careCloud    = new MySdk( $config, $cache );

and in the MySDK:

if ( $this->cache ) {
			$strategy = new DelegatingCacheStrategy( $defaultStrategy = new NullCacheStrategy() );
			foreach ( $this->cache->getRules() as $item ) {
				$strategy->registerRequestMatcher(
					new CacheRequestMatcher( $item ),
					new GreedyCacheStrategy(
						new Psr6CacheStorage( $this->cache->getCacheItemPool() ),
						$item->getTtl() )
				);
			}

			$stack->push( new CacheMiddleware( $strategy ) );
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants