您的位置:首页 > 数据库 > Redis

利用wordpress数据库写API,附加文章redis缓存[菜鸡简单粗暴版]

2016-01-27 18:19 537 查看
前言:

公司接了个活,于是乎老大给了个任务了,给XX做一个个人的APP,用PHP写API,文章发布系统用wordpress,要求有缓存。

看了一下网上的‘wordpress缓存’主要是针对于web端的插件,个人觉得拿来写到API中不是很方便,于是乎自己瞎写了一部分。各位看官大牛,欢迎指正,毕竟第一次用wordpress。

环境要求:

1.wordpress

WordPress 4.4.1版本

2.redis缓存

2.8.8

3.系统

Linux iZ25tyupojmZ 2.6.32-220.23.2.al.ali1.1.alios6.x86_64 #1 SMP Sun Jan 4 15:01:53 CST 2015 x86_64

没错就是阿里云的

4.apach

Apache/2.4.10

5.php

5.5.7

6.mysql

没错也是阿里云的, MySQL5.6

开始吧!

就事论事,这里只写涉及到项目的东西。

1.明确目标:

要求请求之后返回一个文章的:标题,摘要,配图(特色图像),发表时间,最后修改时间,内容

2.寻找数据所在表,以及表之间关系

这个还是上图吧,老规矩,用不到的没在图中显示



3.修改源码,记录操作涉及的文章

这里主要是记录文章的操作信息

主要有new,edit,trash,untrash,delete。

使用redis记录

所以在每个需要记录的文件中都要加上

$redis = new redis();
$redis -> connect('127.0.0.1', 6379);


记录点:

1.新发表的文章

文件:/wp_admin/post-new.php

在最后的两个include之前添加
$new_post_id = $redis->get('new_post');
$new_post_id = json_decode($new_post_id);
$new_post_id[]  = $post_ID;
$redis->set('new_post',json_encode($new_post_id));


2.edit

文件:/wp_admin/post.php

case 'editpost':
...
// Session cookie flag that the post was saved
if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
}

//增加部分
$editpost_id = $redis->get('editpost');
$editpost_id = json_decode($editpost_id);
$editpost_id[]  = $post_id;
$redis->set('editpost',json_encode($editpost_id));
redirect_post($post_id); // Send user on their way while we keep working
//增加部分
exit();


3.trash

文件:/wp_admin/post.php

case 'trash':
...

if ( ! wp_trash_post( $post_id ) )
wp_die( __( 'Error in moving to Trash.' ) );

//增加部分
$trash_id = $redis->get('trash');
$trash_id = json_decode($trash_id);
$trash_id[]  = $post_id;
$redis->set('trash',json_encode($trash_id));
//增加部分
wp_redirect( add_query_arg( array('trashed' => 1, 'ids' => $post_id), $sendback ) );
exit();


4.untrash

文件:/wp_admin/post.php

case 'untrash':
...
if ( ! wp_untrash_post( $post_id ) )
wp_die( __( 'Error in restoring from Trash.' ) );
//增加部分
$untrash_id = $redis->get('untrash');
$untrash_id = json_decode($untrash_id);
$untrash_id[]  = $post_id;
$redis->set('untrash',json_encode($untrash_id));
//增加部分
wp_redirect( add_query_arg('untrashed', 1, $sendback) );
exit();


5.delete

文件:/wp_admin/post.php

case 'delete':
...
if ( $post->post_type == 'attachment' ) {
$force = ( ! MEDIA_TRASH );
if ( ! wp_delete_attachment( $post_id, $force ) )
wp_die( __( 'Error in deleting.' ) );
} else {
if ( ! wp_delete_post( $post_id, true ) )
wp_die( __( 'Error in deleting.' ) );
}
//增加部分
$delete_id = $redis->get('delete');
$delete_id = json_decode($delete_id);
$delete_id[]  = $post_id;
$redis->set('delete',json_encode($delete_id));
//增加部分
wp_redirect( add_query_arg('deleted', 1, $sendback) );
exit();


记录点设置好之后就可以开始写API了

5.API

来个结构图吧



其实这里已经脱离wordpress了,只是用了wordpress的数据。

具体的代码就不贴上了,无非是这么几个操作

缓存操作:

redis->get

redis->set

json_decode

json_encode

数据库操作:

SELECT xx FROM xxx WHERE xx = xx

对,你没看错,就是query查询语句。

OK,结束。

说一些自己的不足,因为第一次使用wordpress,所以对源码了解不多,

在更新缓存中的文章时候,只用缓存记录了更新的ID,并没有记录这篇文章所属的栏目,所以每次更新,就更新所有的栏目。

也是因为没有设置记录栏目的id,所以更新其实就是从数据库中获取所有的数据,或者说是重写缓存,而不是修改缓存。

贴一下代码吧- -。。不知道说的,各位能不能看懂

class api{
public function getPostsCache($term_id){
$trash = json_decode($this->redis->get('trash'));
$untrash = json_decode($this->redis->get('untrash'));
$edit = json_decode($this->redis->get('editpost'));

if (sizeof($trash)+sizeof($edit)+sizeof($untrash) >0) {
//用来记录操作的数组,若长度大于0,说明数据被修改过
$this->updateCache();
}

$data = $this->redis->get('post_' . $term_id);
$data = json_decode($data,TRUE);
return $data;
}

public function updateCache(){
$TERM_ARRAY = $this->TERM_ARRAY;//数组记录了所有栏目的term_id
foreach ($TERM_ARRAY as $key => $value) {
$this->rewriteCache($value);
}
}

public function rewriteCache($term_id){
$this->redis->del('trash');
$this->redis->del('untrash');
$this->redis->del('editpost');
$this->getPostsDB($term_id);//从数据库获取数据
}
}


更新缓存这里,写的挺渣的,希望有大牛可以指正,先谢为敬
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息