From c506e9dbb8de9db34ad3dd38a6ebf745fa0ff4d5 Mon Sep 17 00:00:00 2001 From: Simone Esposito Date: Fri, 27 Dec 2024 16:43:08 +0100 Subject: [PATCH] add Cache layer --- src/cache/CacheInterface.php | 75 +++++++ src/cache/CacheObject.php | 374 +++++++++++++++++++++++++++++++++++ src/cache/RedisClient.php | 118 +++++++++++ 3 files changed, 567 insertions(+) create mode 100644 src/cache/CacheInterface.php create mode 100644 src/cache/CacheObject.php create mode 100644 src/cache/RedisClient.php diff --git a/src/cache/CacheInterface.php b/src/cache/CacheInterface.php new file mode 100644 index 00000000..0c9ac6cd --- /dev/null +++ b/src/cache/CacheInterface.php @@ -0,0 +1,75 @@ +cached_data = $cache_data; + } + + /** + * Restituisce la data dell'ultimo aggiornamento + * @return DateTime + */ + public function getLastUpdate() : DateTime + { + return $this->cached_data['last_update']; + } + + /** + * Configura la data dell'ultimo aggiornamento + * @param DateTime $last_update + * @return void + */ + public function setLastUpdate(DateTime $last_update) : void + { + $this->cached_data['last_update'] = $last_update->format('Y-m-d H:i:s'); + } + + /** + * Restituisce la data in formato Y-m-d + * @return string + */ + public function getDateEvent() : string + { + return $this->cached_data['date_event']; + } + + /** + * Configura il campo date_event + * @param DateTime $date_event + * @return void + */ + public function setDateEvent(DateTime $date_event) : void + { + $this->cached_data['date_event'] = $date_event->format('Y-m-d'); + } + + + /** + * Restituisce l'id del pagamento + * @return string + */ + public function getId() : string + { + return $this->cached_data['id']; + } + + /** + * Configura l'id del pagamento + * @param string $id + * @return void + */ + public function setId(string $id) : void + { + $this->cached_data['id'] = $id; + } + + + /** + * Restituisce il nav del pagamento + * @return string + */ + public function getNav() : string + { + return $this->cached_data['nav']; + } + + public function setNav(string $nav) : void + { + $this->cached_data['nav'] = $nav; + } + + public function getPaEmittente() : string + { + return $this->cached_data['pa_emittente']; + } + + public function setPaEmittente(string $pa_emittente) : void + { + $this->cached_data['pa_emittente'] = $pa_emittente; + } + + public function getIuv() : string + { + return $this->cached_data['iuv']; + } + + public function setIuv(string $iuv) : void + { + $this->cached_data['iuv'] = $iuv; + } + + public function getToken() : string + { + return $this->cached_data['token']; + } + + public function setToken(string $token) : void + { + $this->cached_data['token'] = $token; + } + + public function getIdCarrello() : string + { + return $this->cached_data['id_carrello']; + } + + public function setIdCarrello(string $id_carrello) : void + { + $this->cached_data['id_carrello'] = $id_carrello; + } + + public function getIsGpd() : bool + { + return $this->cached_data['is_gpd']; + } + + public function setIsGpd(bool $is_gpd) : void + { + $this->cached_data['is_gpd'] = $is_gpd; + } + + public function getIsDw() : bool + { + return $this->cached_data['is_dw']; + } + + public function setIsDw(bool $is_dw) : void + { + $this->cached_data['is_dw'] = $is_dw; + } + + public function getIsAca() : bool + { + return $this->cached_data['is_aca']; + } + + public function setIsAca(bool $is_aca) : void + { + $this->cached_data['is_aca'] = $is_aca; + } + + public function getOutcome() : ?string + { + return $this->cached_data['outcome']; + } + + public function setOutcome(?string $outcome) : void + { + $this->cached_data['outcome'] = $outcome; + } + + public function getAmount() : ?float + { + return $this->cached_data['amount']; + } + + public function setAmount(?float $amount) : void + { + $this->cached_data['amount'] = $amount; + } + + public function getPayedBy() : ?string + { + return $this->cached_data['payed_by']; + } + + public function setPayedBy(?string $payed_by) : void + { + $this->cached_data['payed_by'] = $payed_by; + } + + public function getPayedAt() : ?DateTime + { + return $this->cached_data['payed_at']; + } + + public function setPayedAt(?DateTime $payed_at) : void + { + $this->cached_data['payed_at'] = $payed_at; + } + + public function getPayedTouchpoint() : ?string + { + return $this->cached_data['payed_touchpoint']; + } + + public function setPayedTouchpoint(?string $payed_touchpoint) : void + { + $this->cached_data['payed_touchpoint'] = $payed_touchpoint; + } + + public function getPayedMethod() : ?string + { + return $this->cached_data['payed_method']; + } + + public function setPayedMethod(?string $payed_method) : void + { + $this->cached_data['payed_method'] = $payed_method; + } + + public function getPayedWith() : ?string + { + return $this->cached_data['payed_with']; + } + + public function setPayedWith(?string $payed_with) : void + { + $this->cached_data['payed_with'] = $payed_with; + } + + public function getDateWf() : string + { + return $this->cached_data['date_wf']; + } + + public function setDateWf(string $date_wf) : void + { + $this->cached_data['date_wf'] = $date_wf; + } + + + public function getTransfers() : array + { + return $this->cached_data['transfers']; + } + + public function setTransfers(array $transfers) : void + { + $this->cached_data['transfers'] = $transfers; + } + + public function getMetadataPayment() : array + { + return $this->cached_data['metadata_payment']; + } + + public function setMetadataPayment(array $metadata_payment) : void + { + $this->cached_data['metadata_payment'] = $metadata_payment; + } + + public function getMetadataTransfer() : array + { + return $this->cached_data['metadata_transfer']; + } + + public function setMetadataTransfer(array $metadata_transfer) : void + { + $this->cached_data['metadata_transfer'] = $metadata_transfer; + } + + public function getMetadataPAdded() : bool + { + return $this->cached_data['metadata_p_added']; + } + + public function setMetadataPAdded(bool $metadata_p_added) : void + { + $this->cached_data['metadata_p_added'] = $metadata_p_added; + } + + public function getMetadataTAdded() : bool + { + return $this->cached_data['metadata_t_added']; + } + + public function setMetadataTAdded(bool $metadata_t_added) : void + { + $this->cached_data['metadata_t_added'] = $metadata_t_added; + } + + public function getExtraInfo() : array + { + return $this->cached_data['extra_info']; + } + + public function setExtraInfo(array $extra_info) : void + { + $this->cached_data['extra_info'] = $extra_info; + } + + public function getExtraInfoAdded() : bool + { + return $this->cached_data['extra_info_added']; + } + + public function setExtraInfoAdded(bool $extra_info_added) : void + { + $this->cached_data['extra_info_added'] = $extra_info_added; + } + + + /** + * Crea una istanza vuota per una nuova posizione debitoria + * @return CacheObject + */ + public static function createNewInstance() : CacheObject + { + $data = [ + 'last_update' => new DateTime(), + 'date_event' => '', + 'id' => '', + 'nav' => '', + 'pa_emittente' => '', + 'iuv' => '', + 'token' => '', + 'id_carrello' => '', + 'is_gpd' => false, + 'is_dw' => false, + 'is_aca' => false, + 'outcome' => null, + 'amount' => null, + 'payed_by' => null, + 'payed_at' => null, + 'payed_touchpoint' => null, + 'payed_method' => null, + 'payed_with' => null, + 'date_wf' => array(), + 'transfers' => array(), + 'metadata_payment' => array(), + 'metadata_transfer' => array(), + 'metadata_p_added' => false, + 'metadata_t_added' => false, + 'extra_info' => array(), + 'extra_info_added' => false, + ]; + /** + * transfers array + * date-event + * id + * fk_position + * iur + * pa_transfer + * id_transfer + * iban + * is_bollo + * + * metadata_payment + * date-event + * id + * fk-position + * + * quando faccio un update, e metto a true metadata_p_added etc, cancello i valori , tanto è inutile tenerli in memoria + */ + return new self($data); + } + +} \ No newline at end of file diff --git a/src/cache/RedisClient.php b/src/cache/RedisClient.php new file mode 100644 index 00000000..c44b9776 --- /dev/null +++ b/src/cache/RedisClient.php @@ -0,0 +1,118 @@ +client = new Client($connection); + } + + /** + * Restituisce un valore in cache. Se il valore è un json, lo restituisce come array PHP + * @param string $key + * @return mixed + */ + public function get(string $key) : mixed + { + $value = $this->client->get($key); + if (is_null($value)) + { + return null; + } + $decode = json_decode($value, JSON_OBJECT_AS_ARRAY); + return (json_last_error() === JSON_ERROR_NONE) ? $decode : $value; + } + + /** + * Configura un valore $value nella chiave $key. Mantiene la scadenza, che esista o meno (KEEPTTL) + * @param string $key chiave in cache + * @param mixed $value valore da memorizzare. Se si memorizza un array PHP, viene convertito in JSON + * @return void + */ + public function set(string $key, mixed $value) : void + { + $to_add = (is_array($value)) ? json_encode($value) : $value; + $this->client->set($key, $to_add, 'KEEPTTL'); + } + + /** + * Aggiunge un valore all'array presente in $key. + * @param string $key + * @param string $value + * @return void + */ + public function add(string $key, string $value) : void + { + $cached = $this->client->get($key); + // se cached esiste + if ($cached) { + json_decode($cached); + if (json_last_error() === JSON_ERROR_NONE) + { + // è un json + $decode = json_decode($cached, JSON_OBJECT_AS_ARRAY); + $decode[] = $value; + $to_add = json_encode($decode); + } + else + { + $to_add = json_encode(array($cached, $value)); + } + } + else + { + $to_add = json_encode(array($value)); + } + $this->client->set($key, $to_add, 'KEEPTTL'); + } + + + /** + * Elimina una chiave dalla cache + * @param string $key + * @return void + */ + public function delete(string $key) : void + { + $this->client->del($key); + } + + + /** + * Restituisce true/false se una chiave esiste + * @param string $key + * @return bool + */ + public function hasKey(string $key): bool + { + $get = $this->client->get($key); + return !is_null($get); + } + + + /** + * Restituisce tutte le chiavi + * @return array + */ + public function getAllKeys(): array + { + return $this->client->keys('*'); + } + + + /** + * Restituisce il client Redis + * @return Client + */ + public function client() : Client + { + return $this->client; + } +} \ No newline at end of file