koajs / static-cache

[MAINTAINERS WANTED] Static cache for koa
293 stars 47 forks source link

ignore cache for some files which files name match RegExt #76

Closed CntChen closed 6 years ago

CntChen commented 6 years ago

In a case like below:

research

question

can static-cache add an option called ignorefiles or another name, which type is one of RegExp/RegExpArray/filename/filenameArray, and ignore cache for files which match ignorefiles.

generally speaking, static-cache is use for directory/files which are static. So I am not sure this idea is worth and useful.

@dead-horse @fengmk2 @Gorden-Wang

Gorden-Wang commented 6 years ago

@CntChen

Caches the assets on initialization - you need to restart the process to update the assets.(can turn off with options.preload = false)

You can set [option. dynamic = true , option. preload = false ] to solve dynamic situation .

But there too many subdirectories to add xxx/index.html into options.files

How about write a method to generate cache object ...

files: {
            '/sw.goods.js': {
                path: path.join(appInfo.baseDir, 'app/public/sw.goods.js'),
                cacheControl: 'no-cache, max-age=0, no-store, must-revalidate',
                maxAge: 0,
                mime: 'application/javascript',
                type: 'application/javascript',
                mtime: new Date(),
            +   length: fs.statSync(path.join(appInfo.baseDir, 'app/public/sw.goods.js')).size,
            },

        },
CntChen commented 6 years ago

Thx for response!

options.preload=false, don't load files into memory (JS object) when static-cache init. options.dynamic=true during http request, if files not in memory, load file from disk, and add file into cache using options.files strategies.

Generating cache object can deal with too many subdirectories, but cache object is genrated before static-cache init.

My case, when static-cache is running, the subdirectories inside public will increase.

when static-cache init:

./public
├── A
      ├── index.html
      └── index.js

when static-cache running:

./public
├── A
│   ├── index.html
│   └── index.js
├── B
│   ├── index.html
│   └── index.js
└── ...
    ├── index.html
    └── index.js

In this case, options.files include public/A/index.html but not public/B/index.html.

During a http request, use options.ignorefiles to control cache for special files may work.

Gorden-Wang commented 6 years ago

I got it . Let the dynamic files in other folder such as cache/ and config some routers ? app.get('/cache/B/index.html',controller.dynamic)

CntChen commented 6 years ago

Yes, there are other ways to deal with my case, but the way will beyond the range of static-cache. generally speaking, static-cache is use for directory/files which are static.