0 ? $ttl : 0; } if ( $ttl instanceof DateInterval ) { $now = new DateTimeImmutable(); return $now->add( $ttl )->getTimestamp() - $now->getTimestamp(); } return self::DEFAULT_TTL; } /** * {@inheritDoc} * * @since 0.1.0 * * @param string $key Cache key. * @param mixed $default Default value when missing. * @return mixed */ public function get( $key, $default = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound -- Inherited from PSR-16 CacheInterface::get(); cannot rename. $value = get_transient( $this->normalize_key( (string) $key ) ); return false === $value ? $default : $value; } /** * {@inheritDoc} * * @since 0.1.0 * * @param string $key Cache key. * @param mixed $value Value to cache. * @param int|DateInterval|null $ttl TTL. * @return bool */ public function set( $key, $value, $ttl = null ): bool { return (bool) set_transient( $this->normalize_key( (string) $key ), $value, $this->ttl_seconds( $ttl ) ); } /** * {@inheritDoc} * * @since 0.1.0 * * @param string $key Cache key. * @return bool */ public function delete( $key ): bool { return (bool) delete_transient( $this->normalize_key( (string) $key ) ); } /** * {@inheritDoc} * * @since 0.1.0 * * @return bool */ public function clear(): bool { ModelCache::clear(); return true; } /** * {@inheritDoc} * * @since 0.1.0 * * @param iterable $keys Keys to fetch. * @param mixed $default Default value for missing keys. * @return iterable */ public function getMultiple( $keys, $default = null ): iterable { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound -- Inherited from PSR-16 CacheInterface::getMultiple(); cannot rename. $out = array(); foreach ( $keys as $key ) { $out[ (string) $key ] = $this->get( (string) $key, $default ); } return $out; } /** * {@inheritDoc} * * @since 0.1.0 * * @param iterable $values Key/value pairs. * @param int|DateInterval|null $ttl TTL. * @return bool */ public function setMultiple( $values, $ttl = null ): bool { $ok = true; foreach ( $values as $key => $value ) { $ok = $this->set( (string) $key, $value, $ttl ) && $ok; } return $ok; } /** * {@inheritDoc} * * @since 0.1.0 * * @param iterable $keys Keys to delete. * @return bool */ public function deleteMultiple( $keys ): bool { $ok = true; foreach ( $keys as $key ) { $ok = $this->delete( (string) $key ) && $ok; } return $ok; } /** * {@inheritDoc} * * @since 0.1.0 * * @param string $key Cache key. * @return bool */ public function has( $key ): bool { return false !== get_transient( $this->normalize_key( (string) $key ) ); } }