hapijs / catbox

Multi-strategy object caching service
Other
494 stars 72 forks source link

Policy.get value in callback contains item, stored, and ttl keys #160

Closed timcosta closed 7 years ago

timcosta commented 8 years ago

We found a strange issue today where the value argument of the callback to Policy.get was returning an object that still contained the item, ttl, stored, and ttl. Below is (hopefully) all of the relevant code.

Cache initialization:

    serverConfig =
        cache: [
            {
                engine: require('catbox-redis')
                host: redisUrl.hostname
                port: redisUrl.port
                password: redisUrl.auth.split(":")[1] if redisUrl.auth?
                partition: "planner"
            }
        ]
[excluded]
    dataCache = server.cache({ segment: 'data', expiresIn: sessionTime })

The caching implementation used is as follows, where the cache variable is the dataCache from just above:

            findById: (id, callback) ->
                key = "#{config.table}_#{id}"
                cache.get key, (err, data) ->
                    if err?
                        callback err, null
                    else if data?
                        callback null, [data]
                    else
                        Model.findWhere where, (err, data) ->
                            if err?
                                callback err, null
                            else
                                callback null, data
                                if data.length
                                    cache.set key, data.first(), config.ttl, Hoek.ignore

Here is the object we were storing on the last line above:

{ id: 65,
    uuid: '1c23197f-ae6b-4971-bd6d-aef1ee47e565',
    name: 'f-9794-834204295def',
    deleted: 0,
    slug: 'f-9794-834204295def',
    created_at: '2016-03-21 21:09:05',
    last_modified_at: '2016-03-21 21:09:05',
    last_modified_by_id: 'a955ae8b-3ab8-47e3-baeb-9f5c641aee0d' }

The second argument of Policy.get that is documented as being the value was returning the object below:

{ item:
   { id: 65,
     uuid: '1c23197f-ae6b-4971-bd6d-aef1ee47e565',
     name: 'f-9794-834204295def',
     deleted: 0,
     slug: 'f-9794-834204295def',
     created_at: '2016-03-21 21:09:05',
     last_modified_at: '2016-03-21 21:09:05',
     last_modified_by_id: 'a955ae8b-3ab8-47e3-baeb-9f5c641aee0d' },
  stored: 1458594545433,
  ttl: 299993,
  isStale: false }

Any thoughts as to why the observed behavior is not the documented (or at least as I understand it) behavior?

hueniverse commented 8 years ago

Can you reproduce this?

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.