idealley / feathers-hooks-rediscache

Set of caching hooks and routes for feathersjs.
MIT License
38 stars 12 forks source link

ISSUE: results format without paging #23

Closed conghai closed 6 years ago

conghai commented 6 years ago

- Cache working well if has paging enable in Service.

Configs

const options = {
    name: 'service-name',
    Model,
    paginate
}

Results format

{
    "total": 135,
    "limit": 50,
    "skip": 0,
    "data": [...],
    "cache": {
        "cached": true,
        "duration": 900,
        "expiresOn": "2018-02-28T04:44:12.601Z",
        "parent": "xxx",
        "group": "group-xxx",
        "key": "xxx"
    }
} 

- But Service without paging! results format will different the first time (before cache)

Configs

const options = {
    name: 'service-name',
    Model
}

Results format

{
    "cache": {
        "wrapped": [ ... // data cache HERE ... ],
        "cached": true,
        "duration": 900,
        "expiresOn": "2018-02-28T04:44:12.601Z",
        "parent": "xxx",
        "group": "group-xxx",
        "key": "xxx"
}
idealley commented 6 years ago

I do not think it is linked to paginate but to the fact that you are returning an Array instead of an Object. Normally the the hooks should have been able to handle it. I ll dig into it.

Normally it should wrap and un-wrap the data.

idealley commented 6 years ago

I have found the problem. Feathers has changed slightly the way it handles hooks. Before, when you where setting the result property in a before hook it was skipping the service and the the after hooks.

Now it only skips the service, but goes through the remaining hooks. I have slightly amended the way the hooks are handling arrays it should be back to normal with the next release.

if your service returns an [] the cache will work as expected, but will not attached any property to what is returned. Internally an object with the property wrapped will be saved to redis, when such object is retrieved from redis it is unwrapped and only the array is returned. This is to accommodate the issue #10

I hope this solve your issue. Do not hesitate to post back. Note that as Friday evening I am on vacations for two weeks with (almost) no access to internet. I will publish the change ASAP after some more testing.