X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fbookmark%2FBookmarkIO.php;h=f40fa476247ff2fc9fd75c1048c4b7a340516b61;hb=330ac859fb13a3a15875f185a611bfaa6c5f5587;hp=6bf7f3654ebfdd41f87b9468f00c135c2725e9da;hpb=a975d97a8da64864c3c49f1c54f571eb4ea5b81a;p=github%2Fshaarli%2FShaarli.git diff --git a/application/bookmark/BookmarkIO.php b/application/bookmark/BookmarkIO.php index 6bf7f365..f40fa476 100644 --- a/application/bookmark/BookmarkIO.php +++ b/application/bookmark/BookmarkIO.php @@ -1,7 +1,11 @@ conf = $conf; $this->datastore = $conf->get('resource.datastore'); + $this->mutex = $mutex; } /** * Reads database from disk to memory * - * @return BookmarkArray instance + * @return Bookmark[] * * @throws NotWritableDataStoreException Data couldn't be loaded * @throws EmptyDataStoreException Datastore file exists but does not contain any bookmark @@ -67,11 +79,16 @@ class BookmarkIO throw new NotWritableDataStoreException($this->datastore); } + $content = null; + $this->mutex->synchronized(function () use (&$content) { + $content = file_get_contents($this->datastore); + }); + // Note that gzinflate is faster than gzuncompress. // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 $links = unserialize(gzinflate(base64_decode( - substr(file_get_contents($this->datastore), - strlen(self::$phpPrefix), -strlen(self::$phpSuffix))))); + substr($content, strlen(self::$phpPrefix), -strlen(self::$phpSuffix)) + ))); if (empty($links)) { if (filesize($this->datastore) > 100) { @@ -86,7 +103,7 @@ class BookmarkIO /** * Saves the database from memory to disk * - * @param BookmarkArray $links instance. + * @param Bookmark[] $links * * @throws NotWritableDataStoreException the datastore is not writable */ @@ -100,9 +117,13 @@ class BookmarkIO throw new NotWritableDataStoreException(dirname($this->datastore)); } - file_put_contents( - $this->datastore, - self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix - ); + $data = self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix; + + $this->mutex->synchronized(function () use ($data) { + file_put_contents( + $this->datastore, + $data + ); + }); } }