Skip to content

Commit

Permalink
Removed myMapsPathCache
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Hamann <[email protected]>
  • Loading branch information
tacruc committed Jan 3, 2023
1 parent 6152964 commit cc0a8d7
Showing 1 changed file with 10 additions and 49 deletions.
59 changes: 10 additions & 49 deletions lib/Service/MyMapsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ class MyMapsService {

private $logger;
private $root;
private ICacheFactory $cacheFactory;
private \OCP\ICache $myMapsPathsCache;

public function __construct (ILogger $logger, IRootFolder $root, ICacheFactory $cacheFactory) {
public function __construct (ILogger $logger, IRootFolder $root) {
$this->logger = $logger;
$this->root = $root;
$this->cacheFactory = $cacheFactory;
$this->myMapsPathsCache = $this->cacheFactory->createDistributed('maps:myMaps-paths');
}

public function addMyMap($newName, $userId, $counter=0) {
Expand Down Expand Up @@ -105,28 +101,9 @@ public function addMyMap($newName, $userId, $counter=0) {
"sharePermissions"=>$mapFolder->getPermissions(),
]
];
$MyMaps = $this->myMapsPathsCache->get($userId);
if ($MyMaps !== null) {
$MyMaps[] = $MyMap;
$this->myMapsPathsCache->set($userId, $MyMaps, 60 * 60 * 24 );
}
return $MyMap;
}

private function updateMyMapsCache($userId): array{
$userFolder = $this->root->getUserFolder($userId);
$MyMaps = [];
$MyMapsNodes = $userFolder->search(new SearchQuery(
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'application/x-nextcloud-maps'),
0, 0, []));

foreach ($MyMapsNodes as $node) {
$MyMaps[] = $this->node2MyMap($node, $userFolder);
}
$this->myMapsPathsCache->set($userId, $MyMaps, 60 * 60 * 24);
return $MyMaps;
}

private function node2MyMap($node, $userFolder):array{
$mapData = json_decode($node->getContent(), true);
if (isset($mapData["name"])) {
Expand Down Expand Up @@ -168,17 +145,18 @@ private function node2MyMap($node, $userFolder):array{
}

public function getAllMyMaps($userId){
$userFolder = $this->root->getUserFolder($userId);
$MyMaps = [];
$MyMapsNodes = $userFolder->search(new SearchQuery(
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'application/x-nextcloud-maps'),
0, 0, []));

$MyMaps = [];
try {
$MyMaps = null; // $this->myMapsPathsCache->get($userId);
if ($MyMaps === null) {
$MyMaps = $this->updateMyMapsCache($userId);
foreach ($MyMapsNodes as $node) {
if ($node->getName() === ".maps") {
$MyMaps[] = $this->node2MyMap($node, $userFolder);
}
} catch (InvalidPathException | NotFoundException | NotPermittedException | NoUserException $e) {
$this->logger->error($e->getMessage());
}
return $MyMaps;
return $MyMaps;
}

public function updateMyMap($id, $values, $userId) {
Expand All @@ -205,15 +183,6 @@ public function updateMyMap($id, $values, $userId) {
}
}
$file->putContent(json_encode($mapData,JSON_PRETTY_PRINT));
$MyMaps = $this->myMapsPathsCache->get($userId);
if ($MyMaps !== null) {
$MyMap = $this->node2MyMap($file, $userFolder);
$oldKey = array_key_first(array_filter($MyMaps, function ($m) use ($id) {
return $m['id']===$id;
}));
$MyMaps[$oldKey] = $MyMap;
$this->myMapsPathsCache->set($userId, $MyMaps, 60 * 60 * 24);
}
if ($renamed) {
if ($userFolder->nodeExists('/Maps')) {
$mapsFolder = $userFolder->get('/Maps');
Expand All @@ -230,14 +199,6 @@ public function updateMyMap($id, $values, $userId) {

public function deleteMyMap($id, $userId) {
$userFolder = $this->root->getUserFolder($userId);
$MyMaps = $this->myMapsPathsCache->get($userId);
if ($MyMaps !== null) {
$oldKey = array_key_first(array_filter($MyMaps, function ($m) use ($id) {
return $m['id']===$id;
}));
unset($MyMaps[$oldKey]);
$this->myMapsPathsCache->set($userId, $MyMaps, 60 * 60 * 24);
}

$folders = $userFolder->getById($id);
$folder = array_shift($folders);
Expand Down

0 comments on commit cc0a8d7

Please sign in to comment.