summaryrefslogtreecommitdiff
blob: 8cecf8f09d146bc55eac5b57341f10f1d1da2bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\Cache;

use DateTime;
use InvalidArgumentException;

/**
 * Represents a single result from the persistent cache
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2020.12
 */
class PersistentCacheEntry {
	private const MAX_KEY_LENGTH = 255;
	private const MAX_TAG_LENGTH = 255;

	/** @var string */
	private $key;
	/** @var mixed */
	private $value;
	/** @var int|null */
	private $exptime;
	/** @var string|null */
	private $tag;

	public function __construct(
		string $key,
		$value = null,
		int $exptime = null,
		string $tag = null
	) {
		if ( strlen( $key ) > self::MAX_KEY_LENGTH ) {
			throw new InvalidArgumentException(
				"The length of key: $key is greater than allowed " . self::MAX_KEY_LENGTH
			);
		}

		if ( $tag && strlen( $tag ) > self::MAX_TAG_LENGTH ) {
			throw new InvalidArgumentException(
				"The length of tag: $tag is greater than allowed " . self::MAX_TAG_LENGTH
			);
		}

		$this->key = $key;
		$this->value = $value;
		$this->exptime = $exptime;
		$this->tag = $tag;
	}

	public function key(): string {
		return $this->key;
	}

	/** @return mixed */
	public function value() {
		return $this->value;
	}

	public function exptime(): ?int {
		return $this->exptime;
	}

	public function tag(): ?string {
		return $this->tag;
	}

	public function hasExpired(): bool {
		if ( $this->exptime ) {
			return $this->exptime < ( new DateTime() )->getTimestamp();
		}

		return false;
	}
}

class_alias( PersistentCacheEntry::class, '\MediaWiki\Extensions\Translate\PersistentCacheEntry' );