Created basic cache for images.

Provided Image class with copyOf method to allow for creation of new instances when caching images (to stop reference leaks).
This commit is contained in:
Josh Stark 2019-06-02 12:33:27 +01:00
parent bee04db820
commit 49ea41ef3a
3 changed files with 168 additions and 6 deletions

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2019 LinuxServer.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.linuxserver.fleet.cache;
import io.linuxserver.fleet.model.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ImageCache {
private static final Logger LOGGER = LoggerFactory.getLogger(ImageCache.class);
private final Map<Integer, List<Image>> cachedImages;
public ImageCache() {
this.cachedImages = new HashMap<>();
}
public void updateCache(Image image) {
Image updatedImage = Image.copyOf(image);
if (cachedImages.containsKey(updatedImage.getRepositoryId())) {
List<Image> images = cachedImages.get(updatedImage.getRepositoryId());
int cachedImageLocation = images.indexOf(updatedImage);
if (cachedImageLocation > -1) {
LOGGER.info("updateCache({}) Updating existing cached image.", updatedImage.getName());
images.set(cachedImageLocation, updatedImage);
} else {
LOGGER.info("updateCache({}) Adding image to cache", updatedImage.getName());
images.add(updatedImage);
}
} else {
List<Image> images = new ArrayList<>();
images.add(updatedImage);
LOGGER.info("updateCache({}:{}) Creating new cache for repository {}", updatedImage.getName(), updatedImage.getRepositoryId());
cachedImages.put(updatedImage.getRepositoryId(), images);
}
}
public List<Image> getAll(int repositoryId) {
if (cachedImages.containsKey(repositoryId)) {
return cachedImages.get(repositoryId).stream().map(Image::copyOf).collect(Collectors.toList());
}
return new ArrayList<>();
}
public Image get(int repositoryId, String imageName) {
List<Image> images = cachedImages.get(repositoryId);
return images.stream().filter(i -> i.getName().equals(imageName)).findFirst().orElse(null);
}
public void remove(Image image) {
if (cachedImages.containsKey(image.getRepositoryId())) {
List<Image> images = cachedImages.get(image.getRepositoryId());
images.remove(image);
}
}
}

View File

@ -17,6 +17,7 @@
package io.linuxserver.fleet.delegate;
import io.linuxserver.fleet.cache.ImageCache;
import io.linuxserver.fleet.db.dao.ImageDAO;
import io.linuxserver.fleet.db.query.InsertUpdateResult;
import io.linuxserver.fleet.db.query.InsertUpdateStatus;
@ -28,10 +29,13 @@ import java.util.List;
public class ImageDelegate {
private final ImageDAO imageDAO;
private final ImageCache imageCache;
private final ImageDAO imageDAO;
public ImageDelegate(ImageDAO imageDAO) {
this.imageDAO = imageDAO;
this.imageDAO = imageDAO;
this.imageCache = new ImageCache();
}
/**
@ -42,7 +46,21 @@ public class ImageDelegate {
* </p>
*/
public Image findImageByRepositoryAndImageName(int repositoryId, String imageName) {
return imageDAO.findImageByRepositoryAndImageName(repositoryId, imageName);
Image cachedImage = imageCache.get(repositoryId, imageName);
if (null == cachedImage) {
Image image = imageDAO.findImageByRepositoryAndImageName(repositoryId, imageName);
if (null != image) {
imageCache.updateCache(image);
}
return image;
}
return cachedImage;
}
public Image fetchImage(int id) {
@ -50,19 +68,40 @@ public class ImageDelegate {
}
public List<Image> fetchImagesByRepository(int repositoryId) {
return imageDAO.fetchImagesByRepository(repositoryId).getResults();
List<Image> cachedImages = imageCache.getAll(repositoryId);
if (cachedImages.isEmpty()) {
List<Image> images = imageDAO.fetchImagesByRepository(repositoryId).getResults();
images.forEach(imageCache::updateCache);
return images;
}
return cachedImages;
}
public void removeImage(Integer id) {
imageDAO.removeImage(id);
Image existingImage = imageDAO.fetchImage(id);
if (null != existingImage) {
imageDAO.removeImage(id);
imageCache.remove(existingImage);
}
}
public Image saveImage(Image image) throws SaveException {
InsertUpdateResult<Image> result = imageDAO.saveImage(image);
if (result.getStatus() == InsertUpdateStatus.OK)
if (result.getStatus() == InsertUpdateStatus.OK) {
Image updatedImage = result.getResult();
imageCache.updateCache(updatedImage);
return result.getResult();
}
throw new SaveException(result.getStatusMessage());
}

View File

@ -17,6 +17,8 @@
package io.linuxserver.fleet.model;
import java.util.Objects;
/**
* <p>
* Representation of a stored image in the Fleet database. Each image contains
@ -45,6 +47,20 @@ public class Image extends PersistableItem<Image> {
this.repositoryId = repositoryId;
}
public static Image copyOf(Image image) {
Image cloned = new Image(image.getId(), image.repositoryId, image.name);
cloned.version = image.version;
cloned.pullCount = image.pullCount;
cloned.versionMask = image.versionMask;
cloned.unstable = image.unstable;
cloned.hidden = image.hidden;
cloned.deprecated = image.deprecated;
cloned.deprecationReason = image.deprecationReason;
return cloned;
}
public Image(int repositoryId, String name) {
this(null, repositoryId, name);
}
@ -126,4 +142,18 @@ public class Image extends PersistableItem<Image> {
public String getDeprecationReason() {
return deprecationReason;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Image image = (Image) o;
return repositoryId == image.repositoryId && Objects.equals(name, image.name);
}
@Override
public int hashCode() {
return Objects.hash(repositoryId, name);
}
}