doudao7113 2015-04-14 11:24
浏览 189
已采纳

Symfony将数据存储在缓存问题中

I'm struggling with this, I need to store data in the browser's cache. I'm using Symfony 2.6.1, I use the following code:

    if($request->cookies->get('myCookie')){
        /*  do stuff  */

    }else{
        $cookie = new Cookie('myCookie', $myData, 0, '/', null, false, false);            
        $response->headers->setCookie($cookie);           
        $response->setCache(array(
            'etag'          => 'abcdef',
            'last_modified' => new \DateTime(),
            'max_age'       => 600,
            's_maxage'      => 600,
            // 'private'       => false,
            'public'        => true,
        ));
        $response->send(); 

        /* send a response */
    }

I need to 1st check if the cookie "myCookie" exists, if not it should create and store in the browser. My problem is that, when I simply refresh the page to cookie is there, but if I close the browser, when I open again the cookie is not there.

I think that is some configuration/ propriety in the in the array when I set the cookie $response->setCache(array( ... ))

In the official site, there is not many helpful info about this.

http://symfony.com/doc/current/book/http_cache.html#types-of-caches

  • 写回答

1条回答 默认 最新

  • duan5801 2015-04-14 11:46
    关注

    Cookie being gone actually makes sense. You're constructing the cookie with line:

    $cookie = new Cookie('myCookie', $myData, 0, '/', null, false, false); 
    

    The third argument (0) means that it's a session cookie - will be discarded as soon as you close your browser.

    Try something like this:

    // Cookie stays alive for an hour
    $cookie = new Cookie('myCookie', $myData, time() + 3600, '/', null, false, false); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装