donglin7979 2013-10-07 11:14
浏览 51

如何在GAE php中使用memcache或memcached?

I have been using the GAE or Google App Engine for PHP for sometime and use a basic cloudSQL to read data from it. Since it been a few months and inorder to optimize it I wanted to reduce the number of reads the auto fill in the form uses to read names from Table. As of not it is as high as 80k per day which is insanely high for a simple form.

Since Google does not allow me to write to a text file or any other temp file without a bucket ( I don't have bucket .. for that matter why do we need one? ) I figured I use a cache feature that I can have it refresh every day and then do the read queries of from the cache instead of the actual Table hence reducing the number of reads.

That's were I ran into a wall. I am a beginner in this and all though a experienced pro may understand this

https://developers.google.com/appengine/docs/php/memcache/#PHP_PHP_memcache_implementation

I have no idea what it says.

Can any one teach me or tell me how to use the cache feature of GAE? I tried searching for a "How to" but didn't find one.

My autocomplete function looks like this

setAutoComplete("name", "results1", "autocomplete.php?part=");

so it calls autocomplete php whenever a text is entered in the form after a keyup.

And the SQL part of it looks like this

$sql = "SELECT SUBSTRING(NAME,1,300) AS Name FROM `Table` WHERE Name LIKE '{$p}%'";
// Check Query ran sucessfully
$query = mysql_query($sql) or die("Query failed: " . mysql_error() . " Actual query: " . $query);
//Create an array with the results
$results = array();
while ($user = mysql_fetch_object($query)) {
    $results[] = $user -> Name;
    }
//using JSON to encode the array
echo json_encode($results);

Hope this is able to paint a clear picutre. Thank you in advance from me and any one else who might come here looking for a similar answer.

P.S - I am reading PHP Manual not really helping much.

  • 写回答

1条回答 默认 最新

  • douhuan1901 2013-10-09 03:56
    关注

    I used both memcache API's supported in GAE(Memcache and Memcached) .This is the Memcache code.(I assume your query is working properly)

        $data = array();
        $key = 'memcahcetestkey';
        $memcache = new Memcache;
        $data = $memcache->get($key);
        if ($data === false) {
          $data = $results ;//yourQueryResult;
          $memcache->set($key, $data);
        }
    

    Then check your memcache values using $your_memecach_array=$memcache->get($key);

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line