•Chia làm 5 loại dữ liệu khác nhau:
STRING: Có thể là string, integer hoặc float. Redis có thể làm việc với cả string, từng phần của string, cũng như tăng/giảm giá trị của integer, float.
LIST: Danh sách liên kết của các strings. Redis hỗ trợ các thao tác push, pop từ cả 2 phía của list, trim dựa theo offset, đọc 1 hoặc nhiều items của list, tìm kiếm và xóa giá trị.SET: Tập hợp các string (không được sắp xếp). Redis hỗ trợ các thao tác thêm, đọc, xóa từng phần tử, kiểm tra sự xuất hiện của phần tử trong tập hợp. Ngoài ra Redis còn hỗ trợ các phép toán tập hợp, gồm intersect/union/difference.HASH: Lưu trữ hash table của các cặp key-value, trong đó key được sắp xếp ngẫu nhiên, không theo thứ tự nào cả. Redis hỗ trợ các thao tác thêm, đọc, xóa từng phần tử, cũng như đọc tất cả giá trị.
ZSET (sorted set): Là 1 danh sách, trong đó mỗi phần tử là map của 1 string (member) và 1 floating-point number (score), danh sách được sắp xếp theo score này. Redis hỗ trợ thao tác thêm, đọc, xóa từng phần tử, lấy ra các phần tử dựa theo range của score hoặc của string.
•Download, extract and compile Redis with:
$ wget http://download.redis.io/releases/redis-2.8.19.tar.gz
$ tar xzf redis-2.8.19.tar.gz
$ cd redis-2.8.19
$ make
•The binaries that are now compiled are available in the src directory. Run Redis with:
$ src/redis-server
•You can interact with Redis using the built-in client:
$ src/redis-cli
redis>
set foo bar
OK
redis>
get foo
"bar"
•Config with CI
Copy Redis.php file
in libraries to application\libraries in CI. Then, copy redis.php file
in config to
application\config in
CI.
Open autoload.php in
application\config in
CI add more libraries “redis”
like that image:
Thực hiện tạo 1 sample Redis trong Codeigniter:
Tại Controller: tạo file content.php (controller/gallery/content.php)
<?php
Class Content extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('redis','',TRUE);
$this->load->model('gallery/gallery_redis','redis_model');
}
//REDIS
public function redis(){
if ($this->redis_model->checkDB()){ // check connection to Redis
$fields = $this->redis_model->getFields();
if ($fields['orange_click'] == ''){// no fields created so create
$this->redis_model->resetDB();
$fields = $this->redis_model->getFields();
}
$data = array(
'fields' => $fields
);
$col_rand = rand(1,3);
if ($col_rand == '1') $method = 'orange';
if ($col_rand == '2') $method = 'green';
if ($col_rand == '3') $method = 'blue';
$data['color_name'] = $method;
$data['color_click'] = $fields[$data['color_name'].'_click'];
$data['color_show'] = $fields[$data['color_name'].'_show'] +1;
$data['method'] = 'Random: '.ucfirst($data['color_name']);
$data['orange_click'] = $fields['orange_click'];
$data['orange_show'] = $fields['orange_show'];
$data['green_click'] = $fields['green_click'];
$data['green_show'] = $fields['green_show'];
$data['blue_click'] = $fields['blue_click'];
$data['blue_show'] = $fields['blue_show'];
$this->redis_model->increaseShow($data['color_name']);
} else {
echo 'Your Redis Server does not appear to be switched on.';
}
$data['content'] = gallery_url() . 'redis';
$this->load->view('templates/view_template', $data);
}
public function set_click($color=0)
{ // value increase on provided colour
$this->redis_model->increaseClick($color);
redirect(base_url().gallery_url().'content/redis', 'refresh');
}
public function skip()
{ // no value increase
redirect(base_url().gallery_url().'content/redis', 'refresh');
}
public function reset_db(){
$this->redis_model->resetDB();
redirect(base_url().gallery_url().'content/redis', 'refresh');
}
}
Class Content extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('redis','',TRUE);
$this->load->model('gallery/gallery_redis','redis_model');
}
//REDIS
public function redis(){
if ($this->redis_model->checkDB()){ // check connection to Redis
$fields = $this->redis_model->getFields();
if ($fields['orange_click'] == ''){// no fields created so create
$this->redis_model->resetDB();
$fields = $this->redis_model->getFields();
}
$data = array(
'fields' => $fields
);
$col_rand = rand(1,3);
if ($col_rand == '1') $method = 'orange';
if ($col_rand == '2') $method = 'green';
if ($col_rand == '3') $method = 'blue';
$data['color_name'] = $method;
$data['color_click'] = $fields[$data['color_name'].'_click'];
$data['color_show'] = $fields[$data['color_name'].'_show'] +1;
$data['method'] = 'Random: '.ucfirst($data['color_name']);
$data['orange_click'] = $fields['orange_click'];
$data['orange_show'] = $fields['orange_show'];
$data['green_click'] = $fields['green_click'];
$data['green_show'] = $fields['green_show'];
$data['blue_click'] = $fields['blue_click'];
$data['blue_show'] = $fields['blue_show'];
$this->redis_model->increaseShow($data['color_name']);
} else {
echo 'Your Redis Server does not appear to be switched on.';
}
$data['content'] = gallery_url() . 'redis';
$this->load->view('templates/view_template', $data);
}
public function set_click($color=0)
{ // value increase on provided colour
$this->redis_model->increaseClick($color);
redirect(base_url().gallery_url().'content/redis', 'refresh');
}
public function skip()
{ // no value increase
redirect(base_url().gallery_url().'content/redis', 'refresh');
}
public function reset_db(){
$this->redis_model->resetDB();
redirect(base_url().gallery_url().'content/redis', 'refresh');
}
}
Tại models: tạo file gallery_redis.php (models/gallery/gallery_redis.php)
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class Gallery_redis extends CI_Model {
function __construct() {
parent::__construct();
}
function checkDB(){
// var_dump($this->redis);die;
$result = $this->redis->command('PING');
if($result == 'PONG'){
return true;
} else {
return false;
}
}
function getFields(){
$data = array();
$data['orange_click'] = $this->redis->get('orange_click');
$data['orange_show'] = $this->redis->get('orange_show');
$data['green_click'] = $this->redis->get('green_click');
$data['green_show'] = $this->redis->get('green_show');
$data['blue_click'] = $this->redis->get('blue_click');
$data['blue_show'] = $this->redis->get('blue_show');
return $data;
}
function increaseShow($color){
$this->redis->incr($color.'_show');
}
function increaseClick($color){
$this->redis->incr($color.'_click');
}
function resetDB(){
$this->redis->set('orange_click','1');
$this->redis->set('orange_show','1');
$this->redis->set('green_click','1');
$this->redis->set('green_show','1');
$this->redis->set('blue_click','1');
$this->redis->set('blue_show','1');
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class Gallery_redis extends CI_Model {
function __construct() {
parent::__construct();
}
function checkDB(){
// var_dump($this->redis);die;
$result = $this->redis->command('PING');
if($result == 'PONG'){
return true;
} else {
return false;
}
}
function getFields(){
$data = array();
$data['orange_click'] = $this->redis->get('orange_click');
$data['orange_show'] = $this->redis->get('orange_show');
$data['green_click'] = $this->redis->get('green_click');
$data['green_show'] = $this->redis->get('green_show');
$data['blue_click'] = $this->redis->get('blue_click');
$data['blue_show'] = $this->redis->get('blue_show');
return $data;
}
function increaseShow($color){
$this->redis->incr($color.'_show');
}
function increaseClick($color){
$this->redis->incr($color.'_click');
}
function resetDB(){
$this->redis->set('orange_click','1');
$this->redis->set('orange_show','1');
$this->redis->set('green_click','1');
$this->redis->set('green_show','1');
$this->redis->set('blue_click','1');
$this->redis->set('blue_show','1');
}
}
Tại views: tạo file redis.php (views/gallery/redis.php)
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style type="text/css">
::selection{ background-color: #E13300; color: blue; }
::moz-selection{ background-color: #E13300; color: blue; }
::webkit-selection{ background-color: #E13300; color: blue; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
font-size: 19px;
font-weight: normal;
margin: 0 0 10px 0;
padding: 14px 15px 0px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body{
margin: 0 15px 0 15px;
}
p.footer{
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container{
margin: 10px;
border: 1px solid #D0D0D0;
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
p{
margin-left:16px;
}
.buttons{
margin-top:20px;
}
.buttons a{
padding: 20px 40px;
font-size:16px;
background-color:#666666;
margin:10px;
color:#FFFFFF;
font-weight:600;
text-decoration:none;
}
.buttons a.orange_css{
background-color:#FF9900;
}
.buttons a.green_css{
background-color:#006633;
}
.buttons a.blue_css{
background-color:#FFFFFF;
color:#333333;
}
</style>
</head>
<div id="container">
<h1>Redis example</h1>
<div id="body">
<button id = "ch_color"type="button" class="btn btn-danger">Click</button>
<button id = "skip_color"type="button" class="btn btn-warning">Skip</button>
<a class = "btn btn-default"href="<?php echo base_url()?><?php echo gallery_url()?>content/reset_db">Reset</a>
<div style="color: <?php echo $color_name;?>;font-size:30px;height:30px;margin-top:5px;"><?php echo $color_name;?></div>
<div id = "dp_click"style="width: 500px;color: <?php echo $color_name;?>; background:<?php echo $color_name;?>;" ><?php echo $color_name;?></div>
<blockquote>
<div id = "dp_show"style="width: 200px;">Click: <?php echo $color_click;?></div>
<div id = "dp_show"style="width: 200px;">Show: <?php echo $color_show;?></div>
</blockquote>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th></th>
<th>Orange</th>
<th>Green</th>
<th>Blue</th>
</tr>
</thead>
<tbody>
<tr>
<td>Click</td>
<td><?php echo $orange_click;?></td>
<td><?php echo $green_click;?></td>
<td><?php echo $blue_click;?></td>
</tr>
<tr>
<td>Show</td>
<td><?php echo $orange_show;?></td>
<td><?php echo $green_show;?></td>
<td><?php echo $blue_show;?></td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
$(document).ready(function(){
$("#ch_color").click(function(){
// $(this).hide();
$('#ch_color').load("/<?php echo gallery_url(); ?>content/set_click/<?php echo $color_name;?>", {
},
function(str) {
console.log(str);
});
location.reload();
});
$("#skip_color").click(function(){
// $(this).hide();
$('#skip_color').load("/<?php echo gallery_url(); ?>content/skip", {
},
function(str) {
console.log(str);
});
location.reload();
});
});
</script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style type="text/css">
::selection{ background-color: #E13300; color: blue; }
::moz-selection{ background-color: #E13300; color: blue; }
::webkit-selection{ background-color: #E13300; color: blue; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
font-size: 19px;
font-weight: normal;
margin: 0 0 10px 0;
padding: 14px 15px 0px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body{
margin: 0 15px 0 15px;
}
p.footer{
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container{
margin: 10px;
border: 1px solid #D0D0D0;
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
p{
margin-left:16px;
}
.buttons{
margin-top:20px;
}
.buttons a{
padding: 20px 40px;
font-size:16px;
background-color:#666666;
margin:10px;
color:#FFFFFF;
font-weight:600;
text-decoration:none;
}
.buttons a.orange_css{
background-color:#FF9900;
}
.buttons a.green_css{
background-color:#006633;
}
.buttons a.blue_css{
background-color:#FFFFFF;
color:#333333;
}
</style>
</head>
<div id="container">
<h1>Redis example</h1>
<div id="body">
<button id = "ch_color"type="button" class="btn btn-danger">Click</button>
<button id = "skip_color"type="button" class="btn btn-warning">Skip</button>
<a class = "btn btn-default"href="<?php echo base_url()?><?php echo gallery_url()?>content/reset_db">Reset</a>
<div style="color: <?php echo $color_name;?>;font-size:30px;height:30px;margin-top:5px;"><?php echo $color_name;?></div>
<div id = "dp_click"style="width: 500px;color: <?php echo $color_name;?>; background:<?php echo $color_name;?>;" ><?php echo $color_name;?></div>
<blockquote>
<div id = "dp_show"style="width: 200px;">Click: <?php echo $color_click;?></div>
<div id = "dp_show"style="width: 200px;">Show: <?php echo $color_show;?></div>
</blockquote>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th></th>
<th>Orange</th>
<th>Green</th>
<th>Blue</th>
</tr>
</thead>
<tbody>
<tr>
<td>Click</td>
<td><?php echo $orange_click;?></td>
<td><?php echo $green_click;?></td>
<td><?php echo $blue_click;?></td>
</tr>
<tr>
<td>Show</td>
<td><?php echo $orange_show;?></td>
<td><?php echo $green_show;?></td>
<td><?php echo $blue_show;?></td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
$(document).ready(function(){
$("#ch_color").click(function(){
// $(this).hide();
$('#ch_color').load("/<?php echo gallery_url(); ?>content/set_click/<?php echo $color_name;?>", {
},
function(str) {
console.log(str);
});
location.reload();
});
$("#skip_color").click(function(){
// $(this).hide();
$('#skip_color').load("/<?php echo gallery_url(); ?>content/skip", {
},
function(str) {
console.log(str);
});
location.reload();
});
});
</script>
Nguồn tham khảo: http://redis.io/download
https://glynrob.com/database/redis-in-codeigniter/
source sample tại: https://github.com/thjensuit/Redis_in_CI
or source sample reference https://github.com/glynrob/Redis-Codeigniter-NO-AB-Testing

Casino Near Me - Mapyro
ReplyDeleteCasino Near Me - Find Casinos Near Me Near Me 세종특별자치 출장마사지 in Colorado, Great Smoky 제주도 출장마사지 Mountains National Park, Casino Near Me - Find 창원 출장샵 Near Me in 공주 출장샵 Washington, D.C., 제천 출장안마 Best Casino For