1- const md5 = require ( 'md5' ) ;
2- const path = require ( 'path' ) ;
1+ const md5 = require ( 'md5' )
2+ const path = require ( 'path' )
33const {
4- readJsonSync,
5- writeJsonSync,
6- ensureDirSync,
7- pathExistsSync,
8- removeSync
4+ readJsonSync,
5+ writeJsonSync,
6+ ensureDirSync,
7+ pathExistsSync,
8+ removeSync
99} = require ( 'fs-extra' )
1010
1111/**
1212 * @param options
1313 * @constructor
1414 */
15- let File = function ( options = { } ) {
16- if ( ! options . directory ) {
17- throw new Error ( 'No cache directory specified.' ) ;
18- }
15+ const File = function ( options = { } ) {
16+ if ( ! options . directory ) {
17+ throw new Error ( 'No cache directory specified.' )
18+ }
1919
20- this . directory = path . resolve ( options . directory )
20+ this . directory = path . resolve ( options . directory )
2121
22- ensureDirSync ( this . directory ) ;
22+ ensureDirSync ( this . directory )
2323}
2424
2525/**
@@ -30,19 +30,19 @@ let File = function (options = {}) {
3030 * @return {* }
3131 */
3232File . prototype . get = function ( key , def ) {
33- if ( ! pathExistsSync ( this . filename ( key ) ) ) {
34- return def ;
35- }
33+ if ( ! pathExistsSync ( this . filename ( key ) ) ) {
34+ return def
35+ }
3636
37- let entry = readJsonSync ( this . filename ( key ) ) ;
37+ const entry = readJsonSync ( this . filename ( key ) )
3838
39- if ( Date . now ( ) / 1000 > entry . expires ) {
40- this . delete ( key ) ;
39+ if ( Date . now ( ) / 1000 > entry . expires ) {
40+ this . delete ( key )
4141
42- return def ;
43- }
42+ return def
43+ }
4444
45- return entry . value ;
45+ return entry . value
4646}
4747
4848/**
@@ -53,10 +53,10 @@ File.prototype.get = function (key, def) {
5353 * @param {number } ttlSeconds
5454 */
5555File . prototype . set = function ( key , value , ttlSeconds = 60 * 24 * 7 ) {
56- writeJsonSync ( this . filename ( key ) , {
57- expires : ( Date . now ( ) / 1000 ) + ttlSeconds ,
58- value : value
59- } ) ;
56+ writeJsonSync ( this . filename ( key ) , {
57+ expires : ( Date . now ( ) / 1000 ) + ttlSeconds ,
58+ value : value
59+ } )
6060}
6161
6262/**
@@ -65,22 +65,22 @@ File.prototype.set = function (key, value, ttlSeconds = 60 * 24 * 7) {
6565 * @param key
6666 */
6767File . prototype . delete = function ( key ) {
68- removeSync ( this . filename ( key ) ) ;
68+ removeSync ( this . filename ( key ) )
6969}
7070
7171/**
7272 * Clear the cache.
7373 */
7474File . prototype . clear = function ( ) {
75- removeSync ( this . directory ) ;
75+ removeSync ( this . directory )
7676}
7777
7878/**
7979 * @param {string } key
8080 * @return {string }
8181 */
8282File . prototype . filename = function ( key ) {
83- return path . join ( this . directory , md5 ( key ) + '.json' ) ;
83+ return path . join ( this . directory , md5 ( key ) + '.json' )
8484}
8585
86- module . exports = File ;
86+ module . exports = File
0 commit comments