ChocoDeveloper
ChocoDeveloper

Reputation: 14588

Where can I store tmp files in Symfony2?

I read that I can use app/cache or app/logs for tmp files, but it doesn't feel right because they are not gonna be removed unless I do it myself.

For example, I need to store a date in a file (I don't want to use a database for this), and I only need it to be there for 2 or 3 days.

What can I do?

Upvotes: 2

Views: 7867

Answers (2)

RobMasters
RobMasters

Reputation: 4148

Sorry this isn't an answer to the exact question being asked, but I'd suggest that there are better ways of achieving this without resorting to storing a date in a temporary file, even if you don't want to use the database.

Have you considered using Redis or Memcache? There are Symfony2 bundles for both of these that should make life a bit easier, although you'd need to ensure that both are installed and running on your server.

If you were to do this using Redis, for example, you could make use of the EXPIRE command to specify how long you want the value (in this case your date) to exist. Here's the rough idea:

public function yourMethod()
{
    $date = $this->getDate();

    /* ... */
}

protected function getDate()
{
    /** @var $redis \Predis\Client */
    $redis = $this->container->get('snc_redis.default'); // TODO inject as a dependency

    $date = $redis->get('your_key');

    // Will be empty if requested after the key has expired.
    // Set a new date value in the key
    if (empty($date)) {
        $date = '2013-01-17 13:30:00'; // Not sure where you want to get this from
        $redis->set('your_key', $date);

        $secondsToLive = 259200; // 3 days
        $redis->expire('your_key', $secondsToLive);
    }

    return $date;
}

Upvotes: 2

AlterPHP
AlterPHP

Reputation: 12727

Is's not a good practice to let the web server to write inside a bundle. You can do it but you'll have to deal with permissions and it introduces a security risk...

Why not using /tmp (on a UNIX server) that is regularly cleaned or your own tmp dir like /home/ChocoDeveloper/tmp with cron task to clean it ?

Upvotes: 2

Related Questions