Home > Database > Redis > body text

How to use Redis and Memcached in php

WBOY
Release: 2023-06-01 17:46:06
forward
1123 people have browsed it

Differences

1. Redis is a storage database. Memcache can also cache photos.

Redis and Memcache store data in memory and are memory databases. However, Memcache can also cache other things like photos and videos. Redis not only supports simple k/v type data, but also provides storage of data structures such as list, set, and hash.

Expiration strategy, memcache is specified when setting. For example, setkey1008 never expires. redis can be set via expire. For example, expirename10.

Storage security, after memcache is closed, the redis data that disappears can be saved in the disk regularly

Disaster recovery, after the memcache hangs up, the redis data that cannot be recovered can be restored through aof.

Redis supports data backup, that is, data backup in master-slave mode.

Different application scenarios:

2. Redis can create nosql database, news queue, etc. Memcache can also cache SQL statements.

Redis can not only make nosql database, but also news queue, data stack, data cache, etc. Memcache is suitable for caching SQL statements, data sets, user temporary data, delayed query data, sessions, etc.

Example

Connecting to Redis service

<?php
$redis = new redis();  //生成redis类的对象,生成之后可以用这个类里面的方法
$redis->connect('127.0.0.1',6379);  //连接redis的ip地址端口号
$redis->set('redistest','666666'); // 给redistest赋值为666666
echo $redis->get('redistest'); //获取redistest的值
?>
Copy after login

Usage of Memcached

<?php
$memcache = new Memcache;
  $memcache->connect("127.0.0.1",11211) or die("Memcached connected failed");
  echo "Memcached's version: " . $memcache->getVersion() . "<br />";
  $data = array(
  'url' => "http://www.cnblogs.com/wujuntian/",
  'name' => "编程人,在天涯"
  );
  $memcache -> set("info",$data,0,10);
  $info = $memcache->get("info");
  echo '<pre class="brush:php;toolbar:false">';
  print_r($info);
  ?>
Copy after login

The above is the detailed content of How to use Redis and Memcached in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!