summaryrefslogtreecommitdiff
path: root/node_modules/node-file-cache/src/index.ts
blob: 515b4a66f0fbd52b063a5332f4cf777ebe178714 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';

import * as database from 'lowdb';
import * as crypto from 'crypto';
import * as util from 'util';

export interface ICacheOptions
{
    file?: string
    life?: number
}

export interface IRecordOptions
{
    tags?: string[],
    life?: number
}

export interface Record
{
    key: string
    val: any
    life: number
    tags: string[]
}

export function create (options?: ICacheOptions)
{
    return new Cache(options || {});
}

export class Cache 
{
    private config: ICacheOptions;
    private db: any;

    constructor (options?: ICacheOptions)
    {
        this.config = this._merge({
            file: 'store.json',
            life: 3600  // one hour
        }, options || {});

        this.db = database(this.config.file);
        this.db.defaults({
            index: []
        }).value();
    }

    public set = function (key: string, value: any, options?: IRecordOptions): Cache
    {
        let record = this._createRecord(key, value, options || {});

        this.expire(key);   // remove previous
        this.db.get('index').push(record).value();
        return this;
    }

    public get = function (key: string): any
    {
        let record = this.db.get('index').find({ key: key }).value();

        if (!record) return null;

        if (record.life < this._createTimestamp()) {
            this.expire(key);
            return null;    // expired 
        }
        
        return record.val;
    }

    /**
     * Clears all records from cache storage
     */
    public clear = function (): Cache
    {
        this.db.set('index', []).value();
        return this;
    }

    /**
     * Removes records from cache storage
     */
    public expire (value: any): Cache
    {
        const _ = this.db._;
        let removed: string[], 
            staying: Record[];

        switch (true) {
            case util.isFunction(value):
                // remove by filter callback
                removed = this.db.get('index')
                            .filter(value)
                            .map('key')
                            .value();
                break;

            case util.isArray(value):
                // remove by tags
                removed = this.db.get('index')
                            .filter((record) => _.intersection(record.tags, value).length)
                            .map('key')
                            .value();
                break;

            case util.isString(value):
                // remove by key
                removed = this.db.get('index')
                            .filter((record) => record.key === value)
                            .map('key')
                            .value();
                break;

            default: 
                throw new Error('Unsupported expiration method: ' + (typeof value)); 
        }

        staying = this.db.get('index')
                    .filter((record) => removed.indexOf(record.key) < 0)
                    .value();
        this._set(staying);

        return this;
    }

    public size (): number 
    {
        return this.db.get('index').value().length;  
    }

    private _set (records: Record[]): void
    {
        this.db.set('index', records).value();
    }

    private _createRecord (key: string, value: any, options: IRecordOptions): Record
    {
        let tags = options.tags || [];
        let span = options.life || this.config.life;
        let life = span * 1000 + this._createTimestamp();

        return {
            key: key,
            val: value,
            life: life,
            tags: tags
        };
    }

    private _createTimestamp (): number 
    {
        return new Date().getTime();  
    }

    private _merge (a: any|any[], b: any|any[]): any|any[] 
    {
        for (let p in b) {
            try {
                if (b[p].constructor === Object) {
                    a[p] = this._merge(a[p], b[p]);
                } else {
                    a[p] = b[p];
                }
            } catch (e) {
                a[p] = b[p];
            }
        }
        return a;
    }
    
}