php-shared-memory icon indicating copy to clipboard operation
php-shared-memory copied to clipboard

Feat : StorageMemcached

Open Trickscenique opened this issue 6 years ago • 0 comments

`<?php

namespace Fuz\Component\SharedMemory\Storage;

use Fuz\Component\SharedMemory\Entity\StoredEntity;

/**

  • SharedMemory

  • This storage saves and restores the object to synchronize inside Memcached.

  • This class takes all its power if you use in-ram files

  • @see http://www.cyberciti.biz/faq/howto-create-linux-ram-disk-filesystem/

  • @license http://opensource.org/licenses/MIT

  • @author Alain Tiemblo [email protected]

  • @version 2.0 */ class StorageMemcached implements StorageInterface {

    const ACCESS_READ = 'r'; const ACCESS_WRITE = 'w';

    protected $memcache; protected $fd; protected $access;

    public function __construct(\Memcached $memcache, string $id) { $this->memcache = $memcache; $this->id = $id; }

    public function __destruct() { $CachedString = $this->memcache->get($this->id);

     if (is_null($CachedString)) {
         return;
     }
    
     /**
      * Save to file NEON
      */
    
    
    
     $this->memcache->delete($this->id);
    

    }

    /**

    • {@inheritdoc} */ public function openReader() { $this->access = self::ACCESS_READ; }

    /**

    • {@inheritdoc} */ public function openWriter() { $this->access = self::ACCESS_WRITE; }

    /**

    • {@inheritdoc} */ public function getObject() { $CachedString = $this->memcache->get($this->id);

      if (is_null($CachedString) or is_bool($CachedString)) { return null; }

      return unserialize($CachedString); }

    /**

    • {@inheritdoc} */ public function setObject(StoredEntity $object) { $this->memcache->set($this->id, serialize($object)); }

    /**

    • {@inheritdoc} */ public function close() { }

    /**

    • {@inheritdoc} */ public function destroy() { $this->memcache->delete($this->id); }

    public function getName() { return 'memcached'; } } `

Trickscenique avatar Nov 19 '19 22:11 Trickscenique