Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> PSR-6, deferred, to persist a cache item, bulk-set
trzczy
post 29.07.2016, 20:41:32
Post #1





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

Ostrzeżenie: (0%)
-----


Raczej ogarniam PSR-6, ale mam kłopot ze zrozumieniem definicji Deferred. Słownik niestety tu nie starcza, bo jak rozumieć "to persist a cache item", "bulk-set operations". Mógłby ktoś przybliżyć to pojęcie Deferred?
Cytat
Deferred - A deferred cache save indicates that a cache item may not be persisted immediately by the pool. A Pool object MAY delay persisting a deferred cache item in order to take advantage of bulk-set operations supported by some storage engines. A Pool MUST ensure that any deferred cache items are eventually persisted and data is not lost, and MAY persist them before a Calling Library requests that they be persisted. When a Calling Library invokes the commit() method all outstanding deferred items MUST be persisted. An Implementing Library MAY use whatever logic is appropriate to determine when to persist deferred items, such as an object destructor, persisting all on save(), a timeout or max-items check or any other appropriate logic. Requests for a cache item that has been deferred MUST return the deferred but not-yet-persisted item.
http://www.php-fig.org/psr/psr-6/
Z góry dziękuję

Ten post edytował trzczy 29.07.2016, 22:18:31
Go to the top of the page
+Quote Post
by_ikar
post 30.07.2016, 09:26:47
Post #2





Grupa: Zarejestrowani
Postów: 1 798
Pomógł: 307
Dołączył: 13.05.2009
Skąd: Gubin/Wrocław

Ostrzeżenie: (0%)
-----


Zamiast zapisywać "rzecz" w cache za każdym razem kiedy wywołasz save, gdzieś jest lista nie zapisanych jeszcze "rzeczy" i dopiero przy większej (nie określono jak wielkiej) liczbie "rzeczy" które się nie zapisały, lub przy wywołaniu destruct, zapisuje je jednocześnie wszystko co nie zostało zapisane (znaczy jednym requestem/wywołaniem etc). Takie lazy-bulk cache, gdzie nie zapisuje "rzeczy" jednej po drugim, tylko w większych partiach, lub jeżeli nie ma ich więcej, to tyle ile jest kiedy obiekt jest kasowany.

W ten sposób zmniejszasz ilość połączeń do cache które są tworzone, zamiast dla każdej "rzeczy", jest jedno dla wszystkich jednocześnie, z tym że to wciąż dzieje się w obrębie jednego żądania (requesta).

Przykład (tylko nie sugeruj się nim w 100% dlatego że w PHP nie robię kompletnie nic już od kilku lat):

Kod
<?php

class Cache {
    private $connection;
    private $bulk = [];
    
    public function add($key, $item) {
        $this->bulk[$key] = $item;

        if(count($this->bulk) > 1)
        {
            $this->save();
        }
    }

    public function save() {
        $this->connection->mSet($this->bulk);
    }

    public function __destruct() {
        $this->save();
    }
}

$cache = new Cache();
$cache->add('key', 'item');
$cache->add('key2', 'item2');


Ten post edytował by_ikar 30.07.2016, 09:39:39
Go to the top of the page
+Quote Post
trzczy
post 30.07.2016, 12:16:45
Post #3





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

Ostrzeżenie: (0%)
-----


Zrozumiałem. Ale ulga :-)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 23.04.2024 - 15:04