mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
implementing search caching
This commit is contained in:
parent
ebbe4009f5
commit
f53c0f681f
@ -159,7 +159,7 @@ export class SearchManager implements ISearchManager {
|
|||||||
result.photos = photos;
|
result.photos = photos;
|
||||||
}
|
}
|
||||||
|
|
||||||
let directories = await connection
|
const directories = await connection
|
||||||
.getRepository(DirectoryEntity)
|
.getRepository(DirectoryEntity)
|
||||||
.createQueryBuilder("dir")
|
.createQueryBuilder("dir")
|
||||||
.where('dir.name LIKE :text COLLATE utf8_general_ci', {text: "%" + text + "%"})
|
.where('dir.name LIKE :text COLLATE utf8_general_ci', {text: "%" + text + "%"})
|
||||||
|
@ -5,6 +5,8 @@ export module ClientConfig {
|
|||||||
autocompleteEnabled: boolean
|
autocompleteEnabled: boolean
|
||||||
InstantSearchTimeout: number;
|
InstantSearchTimeout: number;
|
||||||
autocompleteCacheTimeout: number;
|
autocompleteCacheTimeout: number;
|
||||||
|
instantSearchCacheTimeout: number;
|
||||||
|
searchCacheTimeout: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SharingConfig {
|
export interface SharingConfig {
|
||||||
@ -54,7 +56,9 @@ export class PublicConfigClass {
|
|||||||
instantSearchEnabled: true,
|
instantSearchEnabled: true,
|
||||||
autocompleteEnabled: true,
|
autocompleteEnabled: true,
|
||||||
InstantSearchTimeout: 3000,
|
InstantSearchTimeout: 3000,
|
||||||
autocompleteCacheTimeout: 1000 * 60 * 60
|
autocompleteCacheTimeout: 1000 * 60 * 60,
|
||||||
|
searchCacheTimeout: 1000 * 60 * 60,
|
||||||
|
instantSearchCacheTimeout: 1000 * 60 * 60
|
||||||
},
|
},
|
||||||
Sharing: {
|
Sharing: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
@ -3,42 +3,99 @@ import {PhotoDTO} from "../../../common/entities/PhotoDTO";
|
|||||||
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
|
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
|
||||||
import {Utils} from "../../../common/Utils";
|
import {Utils} from "../../../common/Utils";
|
||||||
import {Config} from "../../../common/config/public/Config";
|
import {Config} from "../../../common/config/public/Config";
|
||||||
import {AutoCompleteItem} from "../../../common/entities/AutoCompleteItem";
|
import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem";
|
||||||
|
import {SearchResultDTO} from "../../../common/entities/SearchResultDTO";
|
||||||
|
|
||||||
interface AutoCompleteCacheItem {
|
interface CacheItem<T> {
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
items: Array<AutoCompleteItem>;
|
item: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GalleryCacheService {
|
export class GalleryCacheService {
|
||||||
|
|
||||||
private static CONTENT_PREFIX = "content:";
|
private static CONTENT_PREFIX = "content:";
|
||||||
private static AUTO_COMPLETE_PREFIX = "content:";
|
private static AUTO_COMPLETE_PREFIX = "autocomplete:";
|
||||||
|
private static INSTANT_SEARCH_PREFIX = "instant_search:";
|
||||||
|
private static SEARCH_PREFIX = "search:";
|
||||||
|
private static SEARCH_TYPE_PREFIX = ":type:";
|
||||||
|
|
||||||
|
|
||||||
public getAutoComplete(text: string): Array<AutoCompleteItem> {
|
public getAutoComplete(text: string): Array<AutoCompleteItem> {
|
||||||
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text;
|
const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text;
|
||||||
const tmp = localStorage.getItem(key);
|
const tmp = localStorage.getItem(key);
|
||||||
if (tmp != null) {
|
if (tmp != null) {
|
||||||
const value: AutoCompleteCacheItem = JSON.parse(tmp);
|
const value: CacheItem<Array<AutoCompleteItem>> = JSON.parse(tmp);
|
||||||
if (value.timestamp < Date.now() - Config.Client.Search.autocompleteCacheTimeout) {
|
if (value.timestamp < Date.now() - Config.Client.Search.autocompleteCacheTimeout) {
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return value.items;
|
return value.item;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setAutoComplete(text, items: Array<AutoCompleteItem>): void {
|
public setAutoComplete(text: string, items: Array<AutoCompleteItem>): void {
|
||||||
const tmp: AutoCompleteCacheItem = {
|
const tmp: CacheItem<Array<AutoCompleteItem>> = {
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
items: items
|
item: items
|
||||||
};
|
};
|
||||||
localStorage.setItem(GalleryCacheService.AUTO_COMPLETE_PREFIX + text, JSON.stringify(tmp));
|
localStorage.setItem(GalleryCacheService.AUTO_COMPLETE_PREFIX + text, JSON.stringify(tmp));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getInstantSearch(text: string): SearchResultDTO {
|
||||||
|
const key = GalleryCacheService.INSTANT_SEARCH_PREFIX + text;
|
||||||
|
const tmp = localStorage.getItem(key);
|
||||||
|
if (tmp != null) {
|
||||||
|
const value: CacheItem<SearchResultDTO> = JSON.parse(tmp);
|
||||||
|
if (value.timestamp < Date.now() - Config.Client.Search.instantSearchCacheTimeout) {
|
||||||
|
localStorage.removeItem(key);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return value.item;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setInstantSearch(text: string, searchResult: SearchResultDTO): void {
|
||||||
|
const tmp: CacheItem<SearchResultDTO> = {
|
||||||
|
timestamp: Date.now(),
|
||||||
|
item: searchResult
|
||||||
|
};
|
||||||
|
localStorage.setItem(GalleryCacheService.INSTANT_SEARCH_PREFIX + text, JSON.stringify(tmp));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getSearch(text: string, type?: SearchTypes): SearchResultDTO {
|
||||||
|
let key = GalleryCacheService.SEARCH_PREFIX + text;
|
||||||
|
if (typeof type != "undefined") {
|
||||||
|
key += GalleryCacheService.SEARCH_TYPE_PREFIX + type;
|
||||||
|
}
|
||||||
|
const tmp = localStorage.getItem(key);
|
||||||
|
if (tmp != null) {
|
||||||
|
const value: CacheItem<SearchResultDTO> = JSON.parse(tmp);
|
||||||
|
if (value.timestamp < Date.now() - Config.Client.Search.searchCacheTimeout) {
|
||||||
|
localStorage.removeItem(key);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return value.item;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public setSearch(text: string, type: SearchTypes, searchResult: SearchResultDTO): void {
|
||||||
|
const tmp: CacheItem<SearchResultDTO> = {
|
||||||
|
timestamp: Date.now(),
|
||||||
|
item: searchResult
|
||||||
|
};
|
||||||
|
let key = GalleryCacheService.SEARCH_PREFIX + text;
|
||||||
|
if (typeof type != "undefined") {
|
||||||
|
key += GalleryCacheService.SEARCH_TYPE_PREFIX + type;
|
||||||
|
}
|
||||||
|
localStorage.setItem(key, JSON.stringify(tmp));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public getDirectory(directoryName: string): DirectoryDTO {
|
public getDirectory(directoryName: string): DirectoryDTO {
|
||||||
if (Config.Client.enableCache == false) {
|
if (Config.Client.enableCache == false) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -74,43 +74,72 @@ export class GalleryService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: cache
|
|
||||||
public async search(text: string, type?: SearchTypes): Promise<ContentWrapper> {
|
public async search(text: string, type?: SearchTypes): Promise<ContentWrapper> {
|
||||||
|
if (this.searchId != null) {
|
||||||
clearTimeout(this.searchId);
|
clearTimeout(this.searchId);
|
||||||
|
}
|
||||||
if (text === null || text === '' || text.trim() == ".") {
|
if (text === null || text === '' || text.trim() == ".") {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
this.content.next(new ContentWrapper());
|
this.content.next(new ContentWrapper());
|
||||||
const cw: ContentWrapper = await this.networkService.getJson<ContentWrapper>("/search/" + text, {type: type});
|
const cw = new ContentWrapper();
|
||||||
console.log("photos", cw.searchResult.photos.length);
|
cw.searchResult = this.galleryCacheService.getSearch(text, type);
|
||||||
console.log("direcotries", cw.searchResult.directories.length);
|
if (cw.searchResult == null) {
|
||||||
|
const params = {};
|
||||||
|
if (typeof type != "undefined") {
|
||||||
|
params['type'] = type;
|
||||||
|
}
|
||||||
|
cw.searchResult = (await this.networkService.getJson<ContentWrapper>("/search/" + text, params)).searchResult;
|
||||||
|
this.galleryCacheService.setSearch(text, type, cw.searchResult);
|
||||||
|
}
|
||||||
this.content.next(cw);
|
this.content.next(cw);
|
||||||
return cw;
|
return cw;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: cache (together with normal search)
|
|
||||||
public async instantSearch(text: string): Promise<ContentWrapper> {
|
public async instantSearch(text: string): Promise<ContentWrapper> {
|
||||||
if (text === null || text === '' || text.trim() == ".") {
|
if (text === null || text === '' || text.trim() == ".") {
|
||||||
const content = new ContentWrapper();
|
const content = new ContentWrapper(this.lastDirectory);
|
||||||
content.directory = this.lastDirectory;
|
|
||||||
content.searchResult = null;
|
|
||||||
this.content.next(content);
|
this.content.next(content);
|
||||||
|
if (this.searchId != null) {
|
||||||
clearTimeout(this.searchId);
|
clearTimeout(this.searchId);
|
||||||
|
}
|
||||||
|
if (!this.lastDirectory) {
|
||||||
|
this.getDirectory("/");
|
||||||
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.searchId != null) {
|
if (this.searchId != null) {
|
||||||
clearTimeout(this.searchId);
|
clearTimeout(this.searchId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const cw = new ContentWrapper();
|
||||||
|
cw.directory = null;
|
||||||
|
cw.searchResult = this.galleryCacheService.getSearch(text);
|
||||||
|
if (cw.searchResult == null) {
|
||||||
|
//If result is not search cache, try to load load more
|
||||||
this.searchId = setTimeout(() => {
|
this.searchId = setTimeout(() => {
|
||||||
this.search(text);
|
this.search(text);
|
||||||
this.searchId = null;
|
this.searchId = null;
|
||||||
}, Config.Client.Search.InstantSearchTimeout);
|
}, Config.Client.Search.InstantSearchTimeout);
|
||||||
|
|
||||||
const cw = await this.networkService.getJson<ContentWrapper>("/instant-search/" + text);
|
cw.searchResult = this.galleryCacheService.getInstantSearch(text);
|
||||||
|
|
||||||
|
if (cw.searchResult == null) {
|
||||||
|
cw.searchResult = (await this.networkService.getJson<ContentWrapper>("/instant-search/" + text)).searchResult;
|
||||||
|
this.galleryCacheService.setInstantSearch(text, cw.searchResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
this.content.next(cw);
|
this.content.next(cw);
|
||||||
|
|
||||||
|
//if instant search do not have a result, do not do a search
|
||||||
|
if (cw.searchResult.photos.length == 0 && cw.searchResult.directories.length == 0) {
|
||||||
|
if (this.searchId != null) {
|
||||||
|
clearTimeout(this.searchId);
|
||||||
|
}
|
||||||
|
}
|
||||||
return cw;
|
return cw;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ export class GalleryShareComponent implements OnInit, OnDestroy {
|
|||||||
this._notification.success("Url has been copied to clipboard");
|
this._notification.success("Url has been copied to clipboard");
|
||||||
}
|
}
|
||||||
|
|
||||||
hodiModel() {
|
public hodeModal() {
|
||||||
this.childModal.hide();
|
this.childModal.hide();
|
||||||
this.sharing = null;
|
this.sharing = null;
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ export class SettingsService {
|
|||||||
autocompleteEnabled: true,
|
autocompleteEnabled: true,
|
||||||
instantSearchEnabled: true,
|
instantSearchEnabled: true,
|
||||||
InstantSearchTimeout: 0,
|
InstantSearchTimeout: 0,
|
||||||
|
searchCacheTimeout: 1000 * 60 * 60,
|
||||||
|
instantSearchCacheTimeout: 1000 * 60 * 60,
|
||||||
autocompleteCacheTimeout: 1000 * 60 * 60
|
autocompleteCacheTimeout: 1000 * 60 * 60
|
||||||
},
|
},
|
||||||
concurrentThumbnailGenerations: null,
|
concurrentThumbnailGenerations: null,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user