npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular2-cache

v0.1.0

Published

An implementation of cache at Angular2 (4.0.0-rc.5 compatible).

Downloads

45

Readme

angular2-cache

An implementation of cache at Angular2 (4.0.0-rc.5 compatible).

Description

The cache service supports the following types of caching:

  1. ZONE based on NgZone (the analogue of Java ThreadLocal) and the MemoryGlobalCache.
    The NgZoneGlobalCache service and @ZoneCached decorator are accessible for use.
    The zone cache is cleared after the "Zone" area will have finished its work.
  2. MEMORY based on JavaScript Map.
    The MemoryGlobalCache service and @MemoryCached decorator are accessible for use.
    The memory cache is cleared after F5.
  3. STORAGE based on the Window.sessionStorage (in progress)
  4. SESSION based on the Window.sessionStorage (in progress)
  5. FILE based on the chrome.fileSystem (in progress)
  6. INSTANCE based (in progress)

Also, the zoneCachedDate, memoryCachedDate cached date pipes are accessible now for use.

Installation

First you need to install the npm module:

npm install angular2-cache --save

Use

main.ts

We should integrate the cache providers at first.

import {CacheModule} from 'angular2-cache/index';

@NgModule({
    bootstrap: [ApplicationComponent],
    imports: [
        CacheModule,
        ...
    ],
    ...
})
export class ApplicationModule {

	constructor(...) {
	}
}

app.ts

Then you should inject the appropriate the cache service (NgZoneGlobalCache, MemoryGlobalCache, etc..). The each cache service has the public methods for setting configuration (setEnableLogging, setEnable or setCachedValue for setting the not lazy presets values).

import {NgZoneGlobalCache, MemoryGlobalCache} from 'angular2-cache/index';

@Component({...})
export class App {

   constructor(@Inject(NgZoneGlobalCache) protected ngZoneCache:NgZoneGlobalCache,  // If we want to use ZONE cache
               @Inject(MemoryGlobalCache) protected memoryCache:MemoryGlobalCache,  // If we want to use MEMORY cache
               ...) 
   {
       ngZoneCache.setEnableLogging(false);                                         // By default, the smart logger is enabled
       memoryCache.setEnable(false);                                                // By default, the cache is enabled
       
       // We can also warm up the cache at first
       // memoryCache.setCachedValue(new Date('11/11/2020'), 100500);
       ...
   }

Service.ts

import {CacheKeyBuilder, ZoneCached} from 'angular2-cache/index';

export class Service {

    private id:string;              // Identifier of the service ("cloud-1", "cloud-2", ...)
    private expiration:string;      // Expiration date of the service ("Sun Jul 30 2017 03:00:00 GMT+0300 (Russia TZ 2 Standard Time)", ...)
    
    ...
    
    @ZoneCached()
    public getExpirationDate():Date {
        return this.expiration
            ? new Date(this.expiration)
            : null;
    }

    public isExpired():boolean {
        return this.getExpirationDate() !== null                    // The first invoke - the code of <getExpirationDate> is executed
            && this.getExpirationDate() > new Date('12/12/2019');   // The second invoke - the code of <getExpirationDate> is NOT executed, and the result is taken from the cache     
    }

    /**
     * @override
     */
    public toString():string {
        // It's very important to override the toString() if cached method has no input arguments because the engine
        // uses the global cache key for identifying the result of "getExpirationDate()" for the each service instance
        
        return CacheKeyBuilder.make()
            .appendObjectName(this)     // Don't pass the "this" parameter to "append" method into "toString" code section!
            .append(this.getId())
            .build();                   // The composite key: entity type + entity Id
    }
}

Service2.ts

import {CacheKeyBuilder, ZoneCached} from 'angular2-cache/index';

export class Service {

    private id:string;              // Identifier of the service ("cloud-1", "cloud-2", ...)
    private expiration:string;      // Expiration date of the service ("Sun Jul 30 2017 03:00:00 GMT+0300 (Russia TZ 2 Standard Time)", ...)
    
    ...
    
    // The global cache key for the result of "getExpirationDate()" contains product id and uses it automatically
    @ZoneCached()
    public getExpirationDateByProduct(product:Product):Date {
        return this.expiration
            ? new Date(this.expiration)
            : null;
    }

    public isExpiredByProduct(product:Product):boolean {
        return this.getExpirationDateByProduct(product) !== null                     // The first invoke - the code of <getExpirationDate> is executed
            && this.getExpirationDateByProduct(product) > new Date('12/12/2019');    // The second invoke - the code of <getExpirationDate> is NOT executed, and the result is taken from the cache     
    }
}

export class Product {

    private id:string;              // Identifier of the service ("product-1", "product-2", ...)

    /**
     * @override
     */
    public toString():string {
        // It's very important to override the toString() because the engine uses the global cache key for 
        // identifying the product instance
        
        return CacheKeyBuilder.make()
            .appendObjectName(this)     // Don't pass the "this" parameter to "append" method into "toString" code section!
            .append(this.getId())
            .build();                   // The composite key: entity type + entity Id
    }
}

app.html

<span [innerHTML]='"Expires: <strong>{expirationDate}</strong>" | translate: { expirationDate: ( expirationDate | zoneCachedDate: "yyyy-MM-dd" ) }'>
</span>

Publish

npm run deploy

License

Licensed under MIT.