dongmeng1402
dongmeng1402
2011-07-15 03:52
浏览 137
已采纳

PHP变量缓存

I have some data in MySQL database that are static most of the time. They are mainly almost static values like cities, states, and ethnic. I want to cache them in a variable, preferebly in memory, so that I won't need to perform another query to MySQL every time a page loads.

The problem is, my hosting doesn't support memcache nor APC. The only accelerator I could find is eAccelerator, and I don't think it will do what I have in mind.

Is there any way I can do caching? It is http://www.k-disk.net

Thank you

图片转代码服务由CSDN问答提供 功能建议

我在MySQL数据库中有一些数据在大多数情况下都是静态的。 它们主要是几乎静态的价值观,如城市,州和民族。 我想将它们缓存在一个变量中,最好是在内存中,这样每次页面加载时我都不需要对MySQL执行另一个查询。

问题是,我的托管没有 不支持memcache也不支持APC。 我能找到的唯一加速器是eAccelerator,我认为它不会做我的想法。

我有什么办法可以进行缓存吗? 它是 http://www.k-disk.net

谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongsui8162
    dongsui8162 2011-07-16 19:17
    已采纳

    You can use the Shared Memory extension.

    Look this basic sample: http://www.php.net/manual/en/shmop.examples-basic.php

    点赞 评论
  • dtgvl48608
    dtgvl48608 2011-07-15 04:03

    You can create a custom cache class storing and reading data from disk, using file_put_contents() and file_get_contents().

    点赞 评论
  • doutangu4978
    doutangu4978 2011-07-15 05:37

    You could just write a file to your server that saves a serialized php array of variables. Just plug all of your variables into an associative array then serialize and save. Though I honestly don't see why you don't save the variables to a variable table in the database. Its not an expensive operation.

    $myvars = array(
      'how_high_do_i_jump' => 10,
      'which_tv_show_is_best' => 'Glee',
      'enable_caching' => true,
      'mathematical_solution' => 4534.234
    );
    
    $file_str = serialize($myvars);
    
    //Save the file
    file_put_contents('myvars.ser', $myvars);
    
    //To reverse just do this
    $file = file_get_contents('myvars.ser');
    
    $myvars = unserialize($file);
    

    If that doesn't work for you there is a way to get memcache on your shared host if you have SSH access. I actually did this on hostmonster. Here is a walk through on it (though this is not the article I originally used).
    http://andrewpeng.net/posts/2011/06/271273-memcached-and-dreamhost-shared-tutorial.html

    点赞 评论
  • duanqinqian5299
    duanqinqian5299 2011-07-15 07:21

    Caching is a great example of the ubiquitous time-space tradeoff in programming. You can save time by using space to store results. [1]

    There are many ways at many levels to implement cache on a website. Lets look at them starting form the front end and moving towards the backend without getting into too much details.

    HTTP caching

    See How To Optimize Your Site With HTTP Caching

    Application level caching

    This is caching of "expensive to query" database objects. Eg: memcache, caching pages in files etc.

    Op-code cache

    Eg: PHP accelerator, eAccelerator etc.

    Database level cache

    Optimizing the database by tuning its parameters based on the need and machine hardware.

    In your case, I would recommend tweaking around with my.cnf since given enough RAM, MySQL is quite fast. Just try not to pre-optimize.

    点赞 评论

相关推荐