yiisoft / yiisoft/cache-apcu

tests question

Open
#32 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
13
Forks
8
Avg merge
1h 8m
Merged PRs (30d)
1

Description

What steps will reproduce the problem?

I made simple cache (array) and run tests. And its passed. But in my code i dont use ttl.
So in tests case not covered if cache expired.

<?php

namespace Wispoz\ArrayCache;

use Psr\SimpleCache\InvalidArgumentException;

class ArrayCache implements \Psr\SimpleCache\CacheInterface
{
    private const TTL_INFINITY = 0;
    private const TTL_EXPIRED = -1;

    private $cache = [];
    /**
     * @inheritDoc
     */
    public function get(string $key, mixed $default = null): mixed
    {

        $this->validateKey($key);
        if(array_key_exists($key,$this->cache)) {
            return is_object($this->cache[$key]) ? clone $this->cache[$key] :  $this->cache[$key];
        }
        return $default;
    }

    /**
     * @inheritDoc
     */
    public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
    {
        $ttl = $this->normalizeTtl($ttl);
        $this->validateKey($key);
        if($ttl <= self::TTL_EXPIRED) {
            return $this->delete($key);
        }
        $this->cache[$key]  = is_object($value) ? clone $value : $value;
        return true;
    }

    /**
     * @inheritDoc
     */
    public function delete(string $key): bool
    {
        $this->validateKey($key);
        if(array_key_exists($key,$this->cache)){
            unset($this->cache[$key]);
            return true;
        }

        return false;
    }

    /**
     * @inheritDoc
     */
    public function clear(): bool
    {
        $this->cache = [];

        return true;
    }

    /**
     * @inheritDoc
     */
    public function getMultiple(iterable $keys, mixed $default = null): iterable
    {
        $keys = $this->fromIterable($keys);
        $this->validateKeys($keys);
        $output = [];
        foreach ($keys as $key) {
            $key = (string) $key;
            if(array_key_exists($key,$this->cache)) {
                $output[$key] = $this->cache[$key];
            }else {
                $output[$key] = $default;
            }
        }
        return $output;
    }

    /**
     * @inheritDoc
     */
    public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
    {
        $values = $this->fromIterable($values);
        foreach ($values as $key=>$value) {
            $this->set($key,$value,$ttl);
        }
        return true;
    }

    /**
     * @inheritDoc
     */
    public function deleteMultiple(iterable $keys): bool
    {
        $keys = $this->fromIterable($keys);
        $this->validateKeys($keys);
        $this->cache = array_filter($this->cache,static function($value,$key) use($keys) {
            $key = (string) $key;
            return !in_array($key,$keys,true);
        },ARRAY_FILTER_USE_BOTH);
        return  true;
    }
    private function normalizeTtl(null|int|string|\DateInterval $ttl): int
    {
        if($ttl ===null) {
            return  self::TTL_INFINITY;
        }

        if($ttl instanceof \DateInterval) {
            return (new \DateTime('@0'))->add($ttl)->getTimestamp();
        }
        return ((int) $ttl) > 0 ? $ttl : -1 ;
    }
    /**
     * @inheritDoc
     */
    public function has(string $key): bool
    {
        return array_key_exists($key,$this->cache);
    }
    private function fromIterable($iterable) :array
    {
        return $iterable instanceof  \Traversable ? iterator_to_array($iterable): (array) $iterable;
    }

    /**
     * @param string $key
     * @return void
     * @throws \Wispoz\ArrayCache\InvalidArgumentException
     */
    private function validateKey(string $key):void
    {
        if($key === '' || strpbrk($key,'/\@:{}[]')) {
            throw new \Wispoz\ArrayCache\InvalidArgumentException();
        }

    }

    /**
     * @param array $keys
     * @return void
     * @throws \Wispoz\ArrayCache\InvalidArgumentException
     */
    private function validateKeys(array $keys): void
    {
        foreach ($keys as $key) {
            $this->validateKey($key);
        }
    }
}
What is the expected result?

Test failed

What do you get instead?

All test passed

Additional info
Q A
Version 1.0.?
PHP version 8.1
Operating system linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides no repository file or test name; begin by locating the cache implementation and existing tests for TTL behavior. Reproduce the reported case with a positive TTL and verify that an expired entry is treated as missing, then run the relevant test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.