Implementing Model-level caching

前端 未结 8 1503
孤城傲影
孤城傲影 2021-01-31 00:28

I was posting some comments in a related question about MVC caching and some questions about actual implementation came up. How does one implement a Model-level cache that works

相关标签:
8条回答
  • 2021-01-31 01:02

    You should have a seperate Model which does the SQL interfacing directly, eg. for a Customers table: $CustomerModel->GetCustomers($parameter); et cetera. Then, in those models, you can implement caching transparently without having to edit any of your existing MVCs.

    0 讨论(0)
  • 2021-01-31 01:05

    If you where interested in a more transparent caching system for an active records library. You could assign an id to each query then create an associative array of the result. You can store this relation ship statically or ironically in a database.(It's the kind of trade of of caching you have to use more computer power so you can use less computer power sometimes)

    Keeping track of every time the query is run the resulting hash if the result hash is different the new hash is updated. If the hash is the same then it adds to the number of duplicate results. If the desired number of repeat results come up then you cache the results and stop checking the table for an allotted amount of time and or subsequent runs of the query.

    You would have a class that controlled all of this going ons. Functions could include things like

    -start cache checking
    -set threshold
    -cache always
    -cache time life
    -force clear all cache
    -clear this cache for this query
    -we have been death hit with the death laser and need to catch everything(The I hate you wordpress I'm never using you again function I shouldn't have been so lazy and made my own website function)

    This would help to automate much of your process. Also cache rules can be implemented on a model by model basis or to the entire application as a whole.

    This might be slightly more overhead then some cache systems but if you just want to have caching doing its own thing I think it would work well; with out it running to much amok.

    0 讨论(0)
提交回复
热议问题