Angular 2自定义缓存/本地存储
angular 2中是否有用于自定义缓存控制的工具?本地存储 查看了文档,没有找到任何参考资料 作为一个临时解决方案,可以使用jQuery,但比点使用角度Angular 2自定义缓存/本地存储,angular,Angular,angular 2中是否有用于自定义缓存控制的工具?本地存储 查看了文档,没有找到任何参考资料 作为一个临时解决方案,可以使用jQuery,但比点使用角度 关于你也可以看看这个。这是Angular2的状态管理提供者。我不知道它是否适合你的要求 我同意@micronyks的观点,并相信这是开发用于状态管理的角度应用程序的必备软件包 但是,我相信有一个更适合缓存管理的包:包含缓存的方法装饰器和缓存方法(has,get,set)的 以下示例显示了API方法的使用: anyclass.ts ... im
关于你也可以看看这个。这是Angular2的状态管理提供者。我不知道它是否适合你的要求
我同意@micronyks的观点,并相信这是开发用于状态管理的角度应用程序的必备软件包 但是,我相信有一个更适合缓存管理的包:包含
缓存的方法装饰器和缓存方法(has
,get
,set
)的
以下示例显示了API方法的使用:
anyclass.ts
...
import { CacheService } from '@ngx-cache/core';
@Injectable()
export class AnyClass {
constructor(private readonly cache: CacheService) {
// note that CacheService is injected into a private property of AnyClass
}
// will retrieve 'some string value'
getSomeStringValue(): string {
if (this.cache.has('some-string'))
return this.cache.get('some-string');
this.cache.set('some-string', 'some string value');
return 'some string value';
}
}
...
import { Cached, CacheKey } from '@ngx-cache/core';
export class AnyClass {
// will retrieve 'some string value'
@Cached('some-string')
getSomeStringValue(): string {
return 'some string value';
}
@Cached('some-string')
getSomeStringValue2(@CacheKey param1: string): string {
return 'some string value: ' + param1;
}
}
...
// these are the first executions
console.log(anyClass.getSomeStringValue2('p1'));
console.log(anyClass.getSomeStringValue2('p2'));
...
// will retrieve 'some string value: p1' from `CACHE`
console.log(anyClass.getSomeStringValue2('p1'));
// will retrieve 'some string value: p1' from `CACHE`
console.log(anyClass.getSomeStringValue2('p2'));
此示例显示了Cached
方法装饰器和CacheKey
params装饰器的使用:
anyclass.ts
...
import { CacheService } from '@ngx-cache/core';
@Injectable()
export class AnyClass {
constructor(private readonly cache: CacheService) {
// note that CacheService is injected into a private property of AnyClass
}
// will retrieve 'some string value'
getSomeStringValue(): string {
if (this.cache.has('some-string'))
return this.cache.get('some-string');
this.cache.set('some-string', 'some string value');
return 'some string value';
}
}
...
import { Cached, CacheKey } from '@ngx-cache/core';
export class AnyClass {
// will retrieve 'some string value'
@Cached('some-string')
getSomeStringValue(): string {
return 'some string value';
}
@Cached('some-string')
getSomeStringValue2(@CacheKey param1: string): string {
return 'some string value: ' + param1;
}
}
...
// these are the first executions
console.log(anyClass.getSomeStringValue2('p1'));
console.log(anyClass.getSomeStringValue2('p2'));
...
// will retrieve 'some string value: p1' from `CACHE`
console.log(anyClass.getSomeStringValue2('p1'));
// will retrieve 'some string value: p1' from `CACHE`
console.log(anyClass.getSomeStringValue2('p2'));
以下是用于客户端和服务器端缓存的包列表:
- :缓存实用程序
- :SPA/浏览器平台实现
- :服务器平台实现
- :存储实用程序(服务器平台需要)
您是否考虑过浏览器的本地存储
?我考虑过,但之前在Angular1中提供了工具,但是如果您在angular2
中查找$cacheFactory
,我在第2版中没有找到类似的工具。我也找不到。但这个问题可能会有所帮助。