Home Database Mysql Tutorial 利用位运算处理权限分配来优化数据库存储,并且提高运算效率

利用位运算处理权限分配来优化数据库存储,并且提高运算效率

Jun 07, 2016 pm 03:54 PM
optimization distribute use deal with storage improve database Permissions Operation

这个是临阵磨枪的,在现在做的一个OA项目中,由于多权限造成后台静态管理网页泛滥了,现在不得不改进些新的技术,又因为以前的权限表是作为管理员表的外键,给这次修改带来很*烦,所以想到了类与Linux的位运算权限管理方法。 其实微软的API参数很多也是使

这个是临阵磨枪的,在现在做的一个OA项目中,由于多权限造成后台静态管理网页泛滥了,现在不得不改进些新的技术,又因为以前的权限表是作为管理员表的外键,给这次修改带来很*烦,所以想到了类似与Linux的位运算权限管理方法。

其实微软的API参数很多也是使用宏定义好的,然后才可以进行或运算的传参,如一个MessageBox 的参数大致可是这样的,有一个按钮|有提示图标|有提示文字 等等,这样的或运算得到的结果就是功能都包含的结果.

先以Linux的read-1,write-2,executable-4来句例子,大家耍过linux的都知道,ls -l 这个指令就可以显示出linux的详细权限,说一下这样1读2写4执行的原理,因为这里也利用了数学的一点小知识:

一个byte=8bit,可以表示的范围是0-2^8-1,这一个byte的值如果是0,表示没有任何权限,如果是1表示只有读的权限,如果是2表示只有写的权限,如果是3表示有读写权限,以此类推。

这是Linux 的权限管理原理三位表(实际Linux这部分的权限管理只用了三位,不是一个Byte):

\

那为啥这里的权限管理这麽明确那?原因是每一个的取值都是2的指数,说说这样的好处,这样每一个权限是否存在在byte中的值是唯一的,因为每个2的指数都是对应一个bit 位,所以不会重复。

这样设计的好处是啥吗?
首先,这样设计节约空间,在硬盘或者文件开辟较小的空间,然后就是位运算在CPU执行起来效率比普通运算要快的多,直接电位变化,很快得出运算结果。

然后就的分析真正的算法实现了

我这里的权限一共有8个之多,我首先要做的就是取消原来的权限表的外键关系,因为外键这是一对多的关系,在这里已经不再适用,并且如果我简单的在管理员表中添加几个字段来表示多权限,那样看起来有些冗余,所以这里的权限字段还是维持一个字段不变,然后根据不同的值确定他具体具备哪些权限。

具体设计如下:

1-提案权限(2^0)
2-学院初审权限(2^1)
4-学校初审权限(2^2)
8-学校复审权限(2^3)
16-学校终审权限(2^4)
32-网站管理员权限(2^5)
64-相关部门权限(2^6)
128-牵头办理部门权限(2^7)

你可以当我是在假设这些权限,没必要具体理解他,如果是上面这几个权限,只需要一个Byte就够了(八位)

最初的权限默认是1,就是只有提案权限,然后就是重头戏,权限的动态管理:

增加权限:如给他学院初审权限只需要在原来权限或运算2就是行了(原来权限"2)

增加多个权限:原来权限|2|4|8 ,这个值就同时具有了学院,学校初审,学校复审的权限。

删除某个权限:只要让原来权与非一个权限就是OK(原来权限^2,就是删除了2的权限)

删除多个权限:只要原来权限同时与非多个权限就是OK了(原来权限&~2&~4,就是删除原来权限中的2,4权限)

分离出具要的权限:只要让权限集与2的指数,等于某权限项的就是拥有的权限

for i in range(64):
if privilege & 2**i == 2**i:
privilegelist.append(i)

简单的是现实就是加权限就是采用加法,减权限就是采用减法,但是这里的直接采用加减并不提倡,原因是1.加减法效率低,2.加减法容易溢出,比如减的权限权限本来就是不存在的权限,就会报错

注意的问题:
1.在mysql等,其中bigint 就是8字节,最多可以设置八个权限的组合(8×8-1种权限)
2.int 就只能设置4种权限共8x4-1种权限组合

最后说点前台修改数据然后后台修改权限的思路:

简单的几个checkBox 代表不同的权限,注意这里的CheckBox也是按照原有的权限范围循环出来的,

比如权限范围是2^0 ~ 2^7 其实就似乎对应0,1,2,4,8,16,32,64,128这几个数,然后到权限表中查到这几个数对应的中文名字在checkBox中打印出来,更新的时候根据每一的值进行循环或,这样权限集就是修改后的结果了,呵呵

这样后台权限页面的控制也瞬间变得简单了,直接几个大的模板页面就是OK了,当然这时候的模板页面就要设计的复杂一些,每个功能标签都是根据有无权限项循环打印出来的

这种按照位运算区分权限的思路是通用的,并且这里从数据库设计到前台实现都扯了一遍,希望您能有点收获。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Enable root permissions with one click (quickly obtain root permissions) Enable root permissions with one click (quickly obtain root permissions) Jun 02, 2024 pm 05:32 PM

It allows users to perform more in-depth operations and customization of the system. Root permission is an administrator permission in the Android system. Obtaining root privileges usually requires a series of tedious steps, which may not be very friendly to ordinary users, however. By enabling root permissions with one click, this article will introduce a simple and effective method to help users easily obtain system permissions. Understand the importance and risks of root permissions and have greater freedom. Root permissions allow users to fully control the mobile phone system. Strengthen security controls, customize themes, and users can delete pre-installed applications. For example, accidentally deleting system files causing system crashes, excessive use of root privileges, and inadvertent installation of malware are also risky, however. Before using root privileges

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

What are some ways to resolve inefficiencies in PHP functions? What are some ways to resolve inefficiencies in PHP functions? May 02, 2024 pm 01:48 PM

Five ways to optimize PHP function efficiency: avoid unnecessary copying of variables. Use references to avoid variable copying. Avoid repeated function calls. Inline simple functions. Optimizing loops using arrays.

See all articles