CI框架中redis缓存相关操作文件示例代码,ciredis
本文实例讲述了CI框架中redis缓存相关操作文件。分享给大家供大家参考,具体如下:
redis缓存类文件位置:
'ci\system\libraries\Cache\drivers\Cache_redis.php'
<?php /** * CodeIgniter * * An open source application development framework for PHP 5.2.4 or newer * * NOTICE OF LICENSE * * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it * through the world wide web, please send an email to * licensing@ellislab.com so we can send you a copy immediately. * * @package CodeIgniter * @author EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 3.0 * @filesource */ defined('BASEPATH') OR exit('No direct script access allowed'); /** * CodeIgniter Redis Caching Class * * @package CodeIgniter * @subpackage Libraries * @category Core * @author Anton Lindqvist <anton@qvister.se> * @link */ class CI_Cache_redis extends CI_Driver { /** * Default config * * @static * @var array */ protected static $_default_config = array( /* 'socket_type' => 'tcp', 'host' => '127.0.0.1', 'password' => NULL, 'port' => 6379, 'timeout' => 0 */ ); /** * Redis connection * * @var Redis */ protected $_redis; /** * Get cache * * @param string like *$key* * @return array(hash) */ public function keys($key) { return $this->_redis->keys($key); } /** * Get cache * * @param string Cache ID * @return mixed */ public function get($key) { return $this->_redis->get($key); } /** * mGet cache * * @param array Cache ID Array * @return mixed */ public function mget($keys) { return $this->_redis->mget($keys); } /** * Save cache * * @param string $id Cache ID * @param mixed $data Data to save * @param int $ttl Time to live in seconds * @param bool $raw Whether to store the raw value (unused) * @return bool TRUE on success, FALSE on failure */ public function save($id, $data, $ttl = 60, $raw = FALSE) { return ($ttl) ? $this->_redis->setex($id, $ttl, $data) : $this->_redis->set($id, $data); } /** * Delete from cache * * @param string Cache key * @return bool */ public function delete($key) { return ($this->_redis->delete($key) === 1); } /** * hIncrBy a raw value * * @param string $id Cache ID * @param string $field Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hincrby($key, $field, $value = 1) { return $this->_redis->hIncrBy($key, $field, $value); } /** * hIncrByFloat a raw value * * @param string $id Cache ID * @param string $field Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hincrbyfloat($key, $field, $value = 1) { return $this->_redis->hIncrByFloat($key, $field, $value); } /** * lpush a raw value * * @param string $key Cache ID * @param string $value value * @return mixed New value on success or FALSE on failure */ public function lpush($key, $value) { return $this->_redis->lPush($key, $value); } /** * rpush a raw value * * @param string $key Cache ID * @param string $value value * @return mixed New value on success or FALSE on failure */ public function rpush($key, $value) { return $this->_redis->rPush($key, $value); } /** * rpop a raw value * * @param string $key Cache ID * @param string $value value * @return mixed New value on success or FALSE on failure */ public function rpop($key) { return $this->_redis->rPop($key); } /** * brpop a raw value * * @param string $key Cache ID * @param string $ontime 阻塞等待时间 * @return mixed New value on success or FALSE on failure */ public function brpop($key,$ontime=0) { return $this->_redis->brPop($key,$ontime); } /** * lLen a raw value * * @param string $key Cache ID * @return mixed Value on success or FALSE on failure */ public function llen($key) { return $this->_redis->lLen($key); } /** * Increment a raw value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function increment($id, $offset = 1) { return $this->_redis->exists($id) ? $this->_redis->incr($id, $offset) : FALSE; } /** * incrby a raw value * * @param string $key Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function incrby($key, $value = 1) { return $this->_redis->incrby($key, $value); } /** * set a value expire time * * @param string $key Cache ID * @param int $seconds expire seconds * @return mixed New value on success or FALSE on failure */ public function expire($key, $seconds) { return $this->_redis->expire($key, $seconds); } /** * Increment a raw value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hset($alias,$key, $value) { return $this->_redis->hset($alias,$key, $value); } /** * Increment a raw value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hget($alias,$key) { return $this->_redis->hget($alias,$key); } /** * Increment a raw value * * @param string $id Cache ID * @return mixed New value on success or FALSE on failure */ public function hkeys($alias) { return $this->_redis->hkeys($alias); } /** * Increment a raw value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hgetall($alias) { return $this->_redis->hgetall($alias); } /** * Increment a raw value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hmget($alias,$key) { return $this->_redis->hmget($alias,$key); } /** * del a key value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hdel($alias,$key) { return $this->_redis->hdel($alias,$key); } /** * del a key value * * @param string $id Cache ID * @return mixed New value on success or FALSE on failure */ public function hvals($alias) { return $this->_redis->hvals($alias); } /** * Increment a raw value * * @param string $id Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hmset($alias,$array) { return $this->_redis->hmset($alias,$array); } /** * Decrement a raw value * * @param string $id Cache ID * @param int $offset Step/value to reduce by * @return mixed New value on success or FALSE on failure */ public function decrement($id, $offset = 1) { return $this->_redis->exists($id) ? $this->_redis->decr($id, $offset) : FALSE; } /** * Clean cache * * @return bool * @see Redis::flushDB() */ public function clean() { return $this->_redis->flushDB(); } /** * Get cache driver info * * @param string Not supported in Redis. * Only included in order to offer a * consistent cache API. * @return array * @see Redis::info() */ public function cache_info($type = NULL) { return $this->_redis->info(); } /** * Get cache metadata * * @param string Cache key * @return array */ public function get_metadata($key) { $value = $this->get($key); if ($value) { return array( 'expire' => time() + $this->_redis->ttl($key), 'data' => $value ); } return FALSE; } /** * Check if Redis driver is supported * * @return bool */ public function is_supported() { if (extension_loaded('redis')) { return $this->_setup_redis(); } else { log_message('debug', 'The Redis extension must be loaded to use Redis cache.'); return FALSE; } } /** * Setup Redis config and connection * * Loads Redis config file if present. Will halt execution * if a Redis connection can't be established. * * @return bool * @see Redis::connect() */ protected function _setup_redis() { $config = array(); $CI =& get_instance(); if ($CI->config->load('redis', TRUE, TRUE)) { $config += $CI->config->item('redis'); } $config = array_merge(self::$_default_config, $config); $config = !empty($config['redis'])?$config['redis']:$config; $this->_redis = new Redis(); try { if ($config['socket_type'] === 'unix') { $success = $this->_redis->connect($config['socket']); } else // tcp socket { $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); } if ( ! $success) { log_message('debug', 'Cache: Redis connection refused. Check the config.'); return FALSE; } } catch (RedisException $e) { log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')'); return FALSE; } if (isset($config['password'])) { $this->_redis->auth($config['password']); } return TRUE; } /** * Class destructor * * Closes the connection to Redis if present. * * @return void */ public function __destruct() { if ($this->_redis) { $this->_redis->close(); } } } /* End of file Cache_redis.php */ /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《php缓存技术总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

随着网络技术的发展,PHP已经成为了Web开发的重要工具之一。而其中一款流行的PHP框架——CodeIgniter(以下简称CI)也得到了越来越多的关注和使用。今天,我们就来看看如何使用CI框架。一、安装CI框架首先,我们需要下载CI框架并安装。在CI的官网(https://codeigniter.com/)上下载最新版本的CI框架压缩包。下载完成后,解压缩

PHP开发技巧:如何使用Redis缓存MySQL查询结果引言:在Web开发过程中,数据库查询是常见操作之一。然而,频繁的数据库查询会导致性能问题,影响网页的加载速度。为了提高查询效率,我们可以使用Redis作为缓存,将经常被查询的数据放入Redis中,从而减少对MySQL的查询次数,提高网页的响应速度。本文将介绍如何使用Redis缓存MySQL查询结果的开发

PHP是一种流行的编程语言,广泛应用于Web开发。CI(CodeIgniter)框架是PHP中最受欢迎的框架之一,它提供了一整套现成的工具和函数库,以及一些流行的设计模式,让开发人员能够更加高效地开发Web应用程序。本文将介绍使用CI框架开发PHP应用程序的基本步骤和方法。了解CI框架的基本概念和结构在使用CI框架之前,我们需要先了解一些基本的概念和结构。下

Redis在物联网中的应用探索在如今物联网(InternetofThings,IoT)飞速发展的时代,海量的设备连接在一起,为我们提供了丰富的数据资源。而随着物联网应用越来越广泛,大规模数据的处理和存储成为了亟需解决的问题。Redis作为一种高性能的内存数据存储系统,具有出色的数据处理能力和低延迟的特点,为物联网应用带来了很多的优势。Redis是一个开

PHP是一种广泛使用的服务器端脚本语言,而CodeIgniter4(CI4)是一个流行的PHP框架,它提供了一种快速而优秀的方法来构建Web应用程序。在这篇文章中,我们将通过引导您了解如何使用CI4框架,来使您开始使用此框架来开发出众的Web应用程序。1.下载并安装CI4首先,您需要从官方网站(https://codeigniter.com/downloa

随着互联网的发展和不断地融入人们的生活,网络应用的开发变得越来越重要。PHP作为一种众所周知的编程语言,已经成为了开发互联网应用程序的首选语言之一。而开发人员们可以使用众多的PHP框架来简化开发过程,其中最受欢迎的之一是CodeIgniter(CI)框架。CI是一个强大的PHPweb应用框架,它拥有轻量级、简单易用、优化性能等特点,可以让开发人员快速地构建

随着应用程序规模的不断扩大,对数据的需求量也越来越大。缓存作为数据读写的一种优化方式,已经成为现代应用程序中不可或缺的组成部分。在缓存的选择方面,Golang中自带的内存缓存与Redis缓存都是比较常见的选择。本文将对两者进行比较与分析,帮助读者做出更合适的选择。一、内存缓存与Redis缓存的区别数据持久性内存缓存与Redis缓存最大的区别在于数据的持久性。

CI框架中引入CSS样式的步骤如下:1、准备CSS文件;2、将CSS文件存储在CI框架项目的适当位置;3、在需要使用CSS样式的页面中,通过HTML的<link>标签引入CSS文件;4、在HTML元素中使用CSS类或ID名称来应用相应的样式即可。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver Mac版
视觉化网页开发工具