session放入缓存(redis)、DB,sessionredis
session放入缓存(redis)、DB,sessionredis
为什么要把SESSION保存在缓存
就php来说,语言本身支持的session是以文件的方式保存到磁盘文件中,保存在指定的文件夹中,保存的路径可以在配置文件中设置或者在程序中使用函数session_save_path()进行设置,但是这么做有弊端,
1 2 |
|
当然了,保存在缓存中可以解决上面的问题,如果使用php本身的session函数,可以使用session_set_save_handler()函数很方便的对session的处理过程进行重新控制。如果不用php的session系列函数,可以自己编写个类似的session函数,也是可以的,我现在做的这个项目就是这样,会根据用户的mid、登录时间进行求hash作为sessionId,每次请求的时候都必须加上sessionId才算合法(第一次登录的时候是不需要的,这个时候会创建sessionId,返回给客户端),这么做也很方便、简洁高效的。当然了,我这篇文章主要说的是在php自身的SESSION中”做做手脚”。
SESSION保存在缓存中
php将缓存保存到redis中,可以使用配置文件,对session的处理和保存做修改,当然了,在程序中使用ini_set()函数去修改也可以,这个很方便测试,我这里就使用这种方式,当然了,要是生产环境还是建议使用配置文件。
1 2 3 4 5 6 7 8 9 10 11 |
|
这里设置session.save_handler方式为redis,session.save_path为redis的地址和端口,设置之后刷新,再回头查看redis,会发现redis中的生成了sessionId,sessionId和浏览器请求的是一样的,
open(string $savePath, string $sessionName); //open类似于构造函数,开始会话的时候会调用,比如使用session_start()函数之后
close(); //类似于类的析构函数,在write函数调用之后调用,session_write_close()之后之后也会执行
read(string $sessionId); //读取session的时候调用
write(string $sessionId, string $data); //保存数据的时候调用
destory($sessionId); //销毁会话的时候(session_destory()或者session_regenerate_id())会调用
gc($lifeTime); //垃圾清理函数,清理掉过期作废的数据
主要就是实现这几个方法,根据不同的存储驱动可以自己设置不同的具体方法,我实现了mysql数据库和redis这两种保存session的驱动,如果有需要的话可以自己去扩展,扩展很方便很容易。
下面是我的redis的实现(db和redis差不多,redis代码少,贴出来):
我使用了接口的方式,这样扩展起来更方便,那天想用memcached了,直接添加就行了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
看看简单工厂模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
调用也很简单,
1 2 3 4 5 6 |
|
数据库版本的也一样很简单就可以配置,需要的话可以在这里下载完整版和demo
本文版权归作者iforever(luluyrt@163.com)所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.
