开发者

Ajax. Is it possible to not use mysql_query again in a new file?

开发者 https://www.devze.com 2022-12-15 18:32 出处:网络
When I use ajax, I have to use mysql_query again to get information in that file which is for ajax. Can I do action without mysql_query like with include();?

When I use ajax, I have to use mysql_query again to get information in that file which is for ajax. Can I do action without mysql_query like with include();? Ask if you don't understand, because I guess I asked not very properly.

edited:开发者_如何学Go When I try to reach ajax file, in that file I have to retrieve member information again. So, my question is, is it possible to avoid that?


Use a combination of content negotiation and caching, e.g

Controller
    Action
        // check if query is cached
        if(!Cache->hasSaved(Query))
            Result = Query->run
            Cache->save(Result)
        else
            Result = Cache->getSaved(Query)

        // check if Request was done via Ajax
        if(Request->isAjax)
            View->disableLayout
            View->set(JSON->encode(Result))
        else
            View->set(Result)

Like Ignacio explained, each requests to the server is isolated. PHP has a shared nothing architecture, so the only thing you can do to prevent the query from running again is to cache it. Content negotiation just helps to use the same query and return it on the depending request context, so you dont have a Controller Action for Ajax and one for regular calls.


Each access to a PHP page on the server is an isolated action. Results are not cached between accesses unless explicitly cached, e.g. in the session.


You could implement a caching layer, i.e. memcache or APC, which would allow you to store database results by key which would allow you to skip database access on subsequent calls.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号