/**
 * This file is part of the vscode-helpers distribution.
 * Copyright (c) Marcel Joachim Kloubert.
 *
 * vscode-helpers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * vscode-helpers 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */ import * as _ from 'lodash' ; import * as vscode_helpers from '../index' ; /**
 * A cache provider.
 */ export interface CacheProvider { returns chainable clear ( ) : this ; param { any } key param { TValue } defaultValue returns { TValue | TDefault } get < TValue = any , TDefault = TValue > ( key , defaultValue ? ) : TValue | TDefault ; param { string } key return { boolean } has ( key ) : boolean ; param { string } key param { TValue } value returns chainable set < TValue > ( key , value ) : this ; param { string } name param { TValue } value returns chainable unset ( name ) : this ; } /**
 * A basic cache provider.
 */ export abstract class CacheProviderBase implements CacheProvider { inheritdoc public abstract clear ( ) ; inheritdoc public abstract get < TValue = any , TDefault = TValue > ( key , defaultValue ? ) ; inheritdoc public abstract has ( key ) ; inheritdoc public abstract set < TValue > ( key , value ) ; inheritdoc public abstract unset ( key ) ; } /**
 * A cache provider, which stores values in memory.
 */ export class MemoryCache extends CacheProviderBase { /**
     * The storage object with values.
     */ protected _storage : { [ key ] : any ; } = { } ; inheritdoc public clear ( ) { this . _storage = { } ; return this ; } inheritdoc public get < TValue = any , TDefault = TValue > ( key , defaultValue ? ) { key = this . normalizeKey ( key ) ; return this . has ( key ) ? this . _storage [ key ] : defaultValue ; } inheritdoc public has ( key ) { key = this . normalizeKey ( key ) ; return _ . has ( this . _storage , key ) ; } param { any } key return { string } protected normalizeKey ( key ) { return vscode_helpers . normalizeString ( key ) ; } inheritdoc public set < TValue > ( key , value ) { key = this . normalizeKey ( key ) ; this . _storage [ key ] = value ; return this ; } inheritdoc public unset ( key ) { key = this . normalizeKey ( key ) ; delete this . _storage [ key ] ; return this ; } }