I would like to keep my cache keys in a central place.I would like to do it as a constant file.At this moment I have declared cache keys each and every page where it's required.But I need to remove that duplication.How can I do that?
One of cache key declaration:
purchasedOfflineArticlesKey: string = 'myLibraryPurchasedOfflineArticles';
Can you tell me a proper design for this? Do I need to create a class for this and use that class in other places where it needs or any other way? Hope you'll give feedback for this.
Update:
I have cache service like below.
local-cache-service.ts
import { Injectable } from '@angular/core';
import { CacheService } from "ionic-cache";
@Injectable()
export class LocalCacheServiceProvider {
constructor(private cache: CacheService) { }
//get Item
getItem(key: string): Promise<any> {
return this.cache.getItem(key);
}
}
I have used it like this:
offline-articles.ts
private purchasedOfflineArticlesKey: string = 'myLibraryPurchasedOfflineArticles';
constructor(private localCacheService: LocalCacheServiceProvider) {
}
ionViewDidLoad() {
this.localCacheService.getItem(this.purchasedOfflineArticlesKey).then(values => {
this.arrangeMyOfflineArticles(values);
}).catch(reason => {
});
}
Use the readonly modifier to declare constants in a class. When a class field is prefixed with the readonly modifier, you can only assign a value to the property inside of the classes' constructor. Assignment to the property outside of the constructor causes an error.
Use Class with static properties One way to manage constants in TypeScript is to use the class. In the following example, the AppSettings class is created with static properties to define constants. Those properties are applied with readonly modifiers, thus they can not be reassigned.
A class constant is a field that is declared with the static and final keywords. As a reminder, the final keyword indicates that the field reference cannot be changed to point to a different value.
A static class can be defined as a sealed class that cannot be inherited besides being inherited from an Object. static class cannot be instantiated, which means you cannot create the instance variable from the Static Class reference.
In one of my projects, I have defined a namespace for this purpose in a file named constants.ts
. You can do the same. Below is some sample code:
export namespace AppConstants
{
// Class for general global variables.
export class General
{
public static readonly WELCOME_TITLE = 'Welcome to my App';
};
}
In my app where I want to use the constants, I am importing this namespace:
import { AppConstants } from './core/common/constants';
I can access these constants like:
myMethod(){
console.log(AppConstants.General.WELCOME_TITLE);
}
Classes shouldn't be used for something that cannot benefit from class instantiation.
One option is to have them as exports in separate file, this way non-existent keys can be resolved on import:
export const foo = 'foo';
export const bar = 'bar';
Alternatively, cache keys can be declared as object keys, e.g. in the place where cache service is declared.
export const CACHE_KEYS = {
foo: 'foo',
bar: 'bar'
};
@Injectable()
export class LocalCacheServiceProvider {
constructor(private cache: CacheService) { }
getItem(key: keyof typeof CACHE_KEYS): Promise<any> {
return this.cache.getItem(key);
}
}
The service can make use of keyof
constraint to limit the keys to known ones.
Depending on the design of the application, LocalCacheServiceProvider
and key set can be extended per module/component to provide unique set of keys for the unit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With