Skip to content

Cache mechanism causes some trouble #694

Closed

Description

@xidui

I defined a mysql model

{
    id: {type: 'number'}
    some_object: {type:'text'}
}, {
    hooks: {
        beforeSave: function (next){
            this.some_object = JSON.stringify(this.some_object);
            return next();
        },
        afterLoad: function (next){
            this.some_object = JSON.parse(this.some_object);
            return next();
        }
    }
}

Every time I use it, it want the field is already a JSON obj and text in db

Seems that there is a caching mechanism:

  1. I get it by id immediately after I create one.
    I found that the afterLoad will not be called when I do some search thing
  2. which causes the problem:
    the beforeSave hook is called at last, so the some_object in memory is text not obj

how about calling the afterLoad hook no matter it is in cache or not?

Activity

  1. dxg commented on Jan 12, 2016

    @dxg
    Collaborator

    The cache is not a cache but an implementation of the identity pattern.
    It will not improve performance, so you might as well not use it.

  2. xidui commented on Jan 13, 2016

    @xidui
    Author

    @dxg
    How can I call afterLoad every time I use the model.get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions