Welcome Guest, Not a member yet? Register   Sign In
Cache Model interactions
#8

Implementing: Now is more easy to do that.

On the specific use-case of saving the model entities results on the cache could be started as:

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model as BaseModel;

class 
Model extends BaseModel
{
    protected 
$cachePrefix 'modelEntity';
    protected 
$cacheTtl    120;

    public function 
find($id)
    {
        
$cache_key $this->getCacheKey($id);

        if (! 
$data cache($cache_key))
        {
            
$data parent::find($id);

            
cache()->save($cache_key$data$this->cacheTtl);
        }

        return 
$data;
    }

    public function 
insert($databool $returnID true)
    {
        
$result parent::insert($data$returnID);

        if (
$result === true || $returnID && is_numeric($result))
        {
            
$cache_key $this->getCacheKey($this->db->insertID());

            
$data parent::find($this->db->insertID());

            
cache()->save($cache_key$data$this->cacheTtl);
        }

        return 
$result;
    }

    public function 
update($id$data)
    {
        
$result parent::update($id$data);

        if (
$result === true)
        {
            
$cache_key $this->getCacheKey($id);

            
$data parent::find($id);

            
cache()->save($cache_key$data$this->cacheTtl);
        }

        return 
$result;
    }

    public function 
delete($id$purge false)
    {
        
$result parent::delete($id$purge);

        if (
$result === true)
        {
            
cache()->delete($this->getCacheKey($id));
        }

        return 
$result;
    }

    protected function 
getCacheKey($id)
    {
        return 
$this->cachePrefix ':' str_replace('\\'':'get_class($this)) . ':' $id;
    }



Then, just extends the custom Model...

PHP Code:
<?php namespace App\Models\Blog;

use 
App\Models\Model;
use 
App\Models\Admin\Users;

class 
Posts extends Model
{
    protected 
$table 'blog_posts';
    
//protected $returnType    = 'App\Entities\Blog\Post';
    
protected $allowedFields = [
        
'author_id',
        
'title',
        
'description',
        
'content',
        
'image',
        
'meta_keywords',
        
'meta_description',
    ];

    protected 
$afterFind       = [
         
'getAuthor',
    ];


    protected function 
getAuthor($data)
    {
        if (isset(
$data['data']['id']))
        {
            
$users = new Users;

            
$data['data']['author'] = $users->limit(1)->find($data['data']['author_id']);
        }

        return 
$data;
    }


Reply


Messages In This Thread
Cache Model interactions - by natanfelles - 01-02-2018, 06:42 PM
RE: Cache Model interactions - by kilishan - 01-02-2018, 08:37 PM
RE: Cache Model interactions - by natanfelles - 01-02-2018, 10:04 PM
RE: Cache Model interactions - by kilishan - 01-02-2018, 10:12 PM
RE: Cache Model interactions - by puschie - 01-03-2018, 04:15 AM
RE: Cache Model interactions - by natanfelles - 01-15-2018, 05:04 AM
RE: Cache Model interactions - by natanfelles - 01-15-2018, 06:31 AM
RE: Cache Model interactions - by natanfelles - 01-23-2018, 06:00 PM



Theme © iAndrew 2016 - Forum software by © MyBB