【问题标题】:Angular 2 custom caching/local storageAngular 2 自定义缓存/本地存储
【发布时间】:2016-07-08 20:56:45
【问题描述】:

angular 2中是否有任何用于自定义缓存控制的工具?本地存储?

浏览了文档,没有找到任何参考资料。

作为临时解决方案可以使用jQuery,但比使用angular有什么意义呢?

问候

【问题讨论】:

  • 你考虑过浏览器的localStorage吗?
  • 我有,但之前在 angular 1 中提供了工具,但是我在版本 2 中没有找到类似的东西
  • 如果您在angular2 中寻找$cacheFactory 。我也找不到。但是this 的问题可能会有所帮助。

标签: angular


【解决方案1】:

你也可以看看这个。这是 Angular2 的一种状态管理提供程序。我不知道它是否适合您的要求,

https://github.com/ngrx/store

【讨论】:

    【解决方案2】:

    我同意 @micronyks 的观点,并相信 @ngrx/store 是开发 Angular 应用程序时必备的包 - 用于状态管理。

    但是,我相信有一个更适合缓存管理的包:@ngx-cache/core,其中包含Cached 方法装饰器和使用caching API 的缓存方法(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';
      }
    }
    

    这个例子展示了Cached方法装饰器和CacheKey参数装饰器的使用:

    anyclass.ts

    ...
    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'));
    

    以下是客户端和服务器端缓存的软件包列表:

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2017-02-07
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多