Table of Contents
wordpress公告栏数据库缓存版
原理.
福利
Home php教程 php手册 wordpress公告栏数据库缓存版

wordpress公告栏数据库缓存版

Jun 06, 2016 pm 08:08 PM
wordpress Bulletin Board database cache

本来想大概八点二十发,但是没忍住. wordpress公告栏数据库缓存版 完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能 公告栏只展示管理员主评论,不展示子评论 以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句 ?php bulletin

本来想大概八点二十发,但是没忍住.

wordpress公告栏数据库缓存版

  1. 完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能
  2. 公告栏只展示管理员主评论,不展示子评论
  3. 以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句<?php bulletin();?>.
    使用之前请把post_id' => 1884的1884改成你的公告栏页面id
function bulletin(){
if (!$announcement = get_option('clear_buffer')) {
$args = array('post_id' => 1884,'number' => 5,'user_id' => '1',parent => '0');$announcement = ''; $comments = get_comments($args);foreach ($comments as $comment) {$announcement .= '
Copy after login
  • '. human_time_diff(get_comment_date('U',$comment->comment_ID), current_time('timestamp')) . '前'.' ' . get_comment_date('H:i',$comment->comment_ID) . '-comment_ID . '" title= "'. strip_tags($comment->comment_content) . '">'. convert_smilies($comment->comment_content) . '
  • ';}update_option('clear_buffer', $announcement); } echo $announcement; } function clear_buffer_function() { update_option('clear_buffer', ''); // 清空缓存 } add_action('comment_post', 'clear_buffer_function'); // 新评论发布,清空缓存 add_action('edit_comment', 'clear_buffer_function'); // 评论被编辑,清空缓存 add_action('trashed_comment', 'clear_buffer_function'); //评论移至回收站,清空缓存

    原理.

    通过update_option来在数据库中添加表以实现缓存,此代码具有通用性,如果想为其他函数实现缓存,只需参照这个例子来折腾.例如,我公告栏原代码为:

    function bulletin(){
    $args = array('post_id' => 1884,'number' => 5,'user_id' => '1',parent => '0');$announcement = ''; $comments = get_comments($args);foreach ($comments as $comment) {$announcement .= '
    Copy after login
  • '. human_time_diff(get_comment_date('U',$comment->comment_ID), current_time('timestamp')) . '前'.' ' . get_comment_date('H:i',$comment->comment_ID) . '-comment_ID . '" title= "'. strip_tags($comment->comment_content) . '">'. convert_smilies($comment->comment_content) . '
  • ';} echo $announcement; }

    可以对比文章开头那段代码,一起来找茬,找到的茬(也就是我用红色标注的地方)就是数据库缓存通用代码.
    以此类推,还可以缓存更多函数,如果要给不同的函数缓存,只需把其他函数的if (!$announcement = get_option('clear_buffer'))改成if (!$announcement = get_option('clear_buffer2')) ,
    然后把

    function clear_buffer_function() {
    update_option('clear_buffer', '');  
    }
    Copy after login

    改成

    function clear_buffer_function() {
    update_option('clear_buffer', ''); 
    update_option('clear_buffer2', '');
    //以此类推
    }
    Copy after login

    其他地方不动,即可

    福利

    今天的代码有点长有点复杂,为了奖励各位认真看完的小盆友,送上一组福利...
    这不是特效,是镜子太脏了..

    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)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    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)

    PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

    PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

    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

    Caching mechanism and application practice in PHP development Caching mechanism and application practice in PHP development May 09, 2024 pm 01:30 PM

    In PHP development, the caching mechanism improves performance by temporarily storing frequently accessed data in memory or disk, thereby reducing the number of database accesses. Cache types mainly include memory, file and database cache. Caching can be implemented in PHP using built-in functions or third-party libraries, such as cache_get() and Memcache. Common practical applications include caching database query results to optimize query performance and caching page output to speed up rendering. The caching mechanism effectively improves website response speed, enhances user experience and reduces server load.

    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())

    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 to use caching in Golang distributed system? How to use caching in Golang distributed system? Jun 01, 2024 pm 09:27 PM

    In the Go distributed system, caching can be implemented using the groupcache package. This package provides a general caching interface and supports multiple caching strategies, such as LRU, LFU, ARC and FIFO. Leveraging groupcache can significantly improve application performance, reduce backend load, and enhance system reliability. The specific implementation method is as follows: Import the necessary packages, set the cache pool size, define the cache pool, set the cache expiration time, set the number of concurrent value requests, and process the value request results.

    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.

    PHP Redis caching applications and best practices PHP Redis caching applications and best practices May 04, 2024 am 08:33 AM

    Redis is a high-performance key-value cache. The PHPRedis extension provides an API to interact with the Redis server. Use the following steps to connect to Redis, store and retrieve data: Connect: Use the Redis classes to connect to the server. Storage: Use the set method to set key-value pairs. Retrieval: Use the get method to obtain the value of the key.

    See all articles