search
HomeBackend DevelopmentPHP Tutorial请教SMARTY 发帖后clearCache 文章列表的逻辑

文章列表的samrty代码如下

$smarty -> setCacheDir(ROOT_PATH."/cache/forum");$smarty -> display("$module/list.html",$_SERVER["REQUEST_URI"]);

ROOT_PATH = 我的根目录
$module = 我PHP开头会自定当前是什么模组,自己找模板中的子目录

因为文章多, 架构多, 不同的模组也多...所以想到把cache 和 template都做成子目录
template子目录为了方便找文件
cache子目录...是因为怕同一个目录中文件过多,分子目录也会以便我按照不同的模组清理缓存

我在display加入了一句:$_SERVER["REQUEST_URI"]

因为是分页,还有可能是其他的变量

我页面上暂时最多有可能有3个参数  list.php?fid=1&type=20&page=3

fid=栏目  (必定有)
type=主题分类   (看情况, 有些页面有, 有些页面没有)
page=当前页面  (必定有)

因为参数不同,所以也可能生成不同的文件

使用smarty主要就是为了减轻数据库压力



问题一.

我在一个名为post.php的页面发表文章

发表完成时会有一些基本的数据,好比$fid = 1 (栏目id),我就想用此栏目id,把当然栏目的cache 清理

就是这个: list.php?fid=1&type=20&page=3

有办法有所有fid = 1有关的都清理吗?

就是只要fid = 1 ,不管其他参数type=100还是type=5,还有所有page=xxx的都更新吗?



问题二.
SMARTY官方 和网上搜到的简介....
清空CACHE只有这一句
clear_cache('index.tpl',cache_id);//清除指定id的缓存
 

并不能清空指定的目录
因为上面也解释过我是有用模板子目录的



问题三.
经验一问

如果我想更实时一点,网站又蛮多人上的,我把解决问题一和问题二,反而把samrty cache时间设成10秒

那如果是一个长期有500~2000人的网站,用户常常留言/交流的网站

我设成10秒左右的缓存会不会比起不缓存更大压力???

我这种情况大家会把流量写得更...每次回帖/发文章都清空缓存,还是直接放弃缓存?



不好意思,已经没什么分献给各位大哥了

诚心求学


回复讨论(解决方案)

一直变化的不建议使用缓存,也可以采用像QQ空间那样用ajax来加载。这个只是我的愚见。

比?奇怪,既然你比?注重的是??,那就不?用?存了。
即使要用?存,那都需要每?行完一?事件後,??的?存就必?重新生成。才能?到??的效果。

?於文章列表,如果你一天?,每分?都?增加或修改新文章的。那?存的作用就不大。

如果是一天?,可能新增的文章?修改?不多。那可以在後?做一?功能。?用?清一次列表的?存。

其?保存?存的路?,目???你都知道,完全可以自己?一?清cache的???啊,?甚?要??用smarty自?的呢?
smarty是按cacheid做判?的,不?合你的情?。

比较奇怪,既然你比较注重的是实时,那就不应用缓存了。
即使要用缓存,那都需要每执行完一个事件后,对应的缓存就必须重新生成。才能达到实时的效果。

对于文章列表,如果你一天内,每分钟都会增加或修改新文章的。那缓存的作用就不大。

如果是一天内,可能新增的文章与修改数不多。那可以在后台做一个功能。调用则清一次列表的缓存。

其实保存缓存的路径,目录规则你都知道,完全可以自己写一个清cache的类实现啊,为甚么要纠结用smarty自带的呢?
smarty是按cacheid做判断的,不适合你的情况。


所以我在想

我的列表像论坛一样,按照最新回覆排列

如果每分钟...甚至每10~20秒都有人回覆.....而列表又有30~40页的话

每10~20秒都要清理一次CACHE

不管是 回覆文章后的满足条件而清理还是自动设定CACHE有效时间为15秒

要不停的清理缓存文件...然后而生成缓存....是否都比起不缓存更加重主机负担??? 

自推一把,求建议

列表页缓存应在相关文章添加时删除
列表页缓存可在再次访问时自动生成,也可在文章发布后主动生成
如果是采用后者,那么列表页可直接为静态,或缓存永不过期。以减少检查缓存是否过期的开销

列表页缓存应在相关文章添加时删除
列表页缓存可在再次访问时自动生成,也可在文章发布后主动生成
如果是采用后者,那么列表页可直接为静态,或缓存永不过期。以减少检查缓存是否过期的开销


谢谢你
但如果大约每分钟都有2~3个回覆

列表是以最新回覆排序

如果列表已有30~40分
每次排序不是也要删除再生成30~40页页吗?

你建议使用CACHE吗?

你的列表是论坛的帖子列表吗?这一点你并没有说明噢
如果是,在不需要缓存
如果是文章列表,可以缓存。因为回覆不影响列表顺序。至多影响到“最新动态”列表

主动生成列表页或清除缓存时,都是以导航条跨距为限的
因为访问超出导航条跨距的页面时,缓存早就已经过期了

如果两三分钟就用新文章上线,就表示这个网站活动频繁。所以缓存就是非常必要的了

你的列表是论坛的帖子列表吗?这一点你并没有说明噢
如果是,在不需要缓存
如果是文章列表,可以缓存。因为回覆不影响列表顺序。至多影响到“最新动态”列表

主动生成列表页或清除缓存时,都是以导航条跨距为限的
因为访问超出导航条跨距的页面时,缓存早就已经过期了

如果两三分钟就用新文章上线,就表示这个网站活动频繁。所以缓存就是非常必要的了


其实系统和论坛几乎就是一样了

因为其他功能关系到会员权和积分,所以没用别人的,只能自己做个简单的

是自己用PHP+SMARTY做的

如果是论坛列表就不用缓存了?

只要回覆会影响到排名...就不建议做缓存了是吧?


顺道一问,不知大家了不了解,DZ,PW他们是不是只有首页做了cache,列表文章都没有cache的?

这与你的具体应用是有关系的
论坛总是要把新帖和有新回复的放在前面,以引起注意
所以缓存就没有意义了

这与你的具体应用是有关系的
论坛总是要把新帖和有新回复的放在前面,以引起注意
所以缓存就没有意义了

谢谢

明白了
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.