运行PHP函数时,清除AJAX加载上的缓存
我在WordPress中有一个通过AJAX加载的页面。图像(每页只有一个)和页面内容像往常一样被缓存。图像是使用PHP函数自动生成的 我见过很多从不缓存加载内容的解决方案——加载时通常会在URL末尾添加日期时间或随机数 但是,通常,我希望缓存结果——当然,这样可以节省时间和内存 每当保存页面或将新用户作为post meta添加到页面时,都会使用以下代码自动生成每个页面的图像:运行PHP函数时,清除AJAX加载上的缓存,php,jquery,ajax,wordpress,caching,Php,Jquery,Ajax,Wordpress,Caching,我在WordPress中有一个通过AJAX加载的页面。图像(每页只有一个)和页面内容像往常一样被缓存。图像是使用PHP函数自动生成的 我见过很多从不缓存加载内容的解决方案——加载时通常会在URL末尾添加日期时间或随机数 但是,通常,我希望缓存结果——当然,这样可以节省时间和内存 每当保存页面或将新用户作为post meta添加到页面时,都会使用以下代码自动生成每个页面的图像: function group_thumbnail($post_id) { // Generates a
function group_thumbnail($post_id) {
// Generates a composite thumbnail, a la Spotify playlists, to use in the group layout.
global $pagenow;
if(get_post_type($post_id)=='group' && $pagenow != 'post-new.php' && $_GET['action']!='trash') : // Post new has to be excluded, or it tries to do this with nothing in it.
$user_portraits = array();
if(!empty($_POST['wpcf']['allowed-users'])) :
$allowed_users = $_POST['wpcf']['allowed-users']; // For the back end
else :
$allowed_users = get_post_meta($post_id,'wpcf-allowed-users',false); // For the front end
endif;
$group_size = count($allowed_users);
if($group_size < 4) : $limit=1; $row_size = 1; endif;
if($group_size >= 4 && $group_size < 9) : $limit=4; $row_size = 2; endif;
if($group_size >= 9 && $group_size < 16) : $limit=9; $row_size = 3; endif;
if($group_size >= 16) : $limit=16; $row_size = 4; endif;
$square_size = 200/$row_size;
foreach ($allowed_users as $u_id) :
$u_portrait=get_user_meta($u_id, 'wpcf-portrait', true);
$u_file = imagecreatefromjpeg($u_portrait);
$user_portraits[] = $u_file;
endforeach;
$group_image = imagecreatetruecolor ( 200 , 200 );
$postion = array('x'=>0,'y'=>0);
for($i=0; $i<$limit; $i++) :
if($i % $row_size == 0 && $i<>0) :
$postion['x']=0;
$postion['y']+=$square_size;
endif;
imagecopyresized($group_image,$user_portraits[$i],$postion['x'],$postion['y'],0,0,$square_size,$square_size,180,180);
imagedestroy($user_portraits[$i]);
$postion['x']+=$square_size;
endfor;
// Output and free from memory
imagejpeg($group_image,get_template_directory().'/img/groups/'.$post_id.'.jpg',60);
imagedestroy($group_image);
endif;
}
function group\u缩略图($post\u id){
//生成一个组合缩略图,即la Spotify播放列表,以在组布局中使用。
全球$pagenow;
如果(get_post_type($post_id)='group'&&&&$pagenow!='post new.php'&&&$'u get['action']!='trash')://必须排除post new,否则它会尝试在没有任何内容的情况下执行此操作。
$user_肖像=数组();
如果(!empty($_POST['wpcf']['allowed-users']):
$allowed\u users=$\u POST['wpcf']['allowed-users']];//用于后端
其他:
$allowed_users=get_post_meta($post_id,'wpcf-allowed-users',false);//用于前端
endif;
$group\U size=计数($allowed\U用户);
如果($group_size<4):$limit=1;$row_size=1;endif;
如果($group\u size>=4&&$group\u size<9):$limit=4;$row\u size=2;endif;
如果($group\u size>=9&&$group\u size<16):$limit=9;$row\u size=3;endif;
如果($group\U size>=16):$limit=16;$row\U size=4;endif;
$square\u size=200/$row\u size;
foreach($u用户作为$u\u id):
$u_-grait=get_-user_-meta($u-id,'wpcf-grait',true);
$u_file=imagecreatefromjpeg($u_肖像);
$user\u肖像[]=$u文件;
endforeach;
$group_image=ImageCreateTureColor(200200);
$position=array('x'=>0,'y'=>0);
对于($i=0;$i“是否只有在触发此函数时才能清除缓存?”–不,没有。浏览器在缓存资源时如何对待资源,主要取决于它上次请求该资源时收到的缓存头。现在,除非你有时间机器,否则你不能追溯更改。如果我有时间机器,缓存将是我最不想做的事情。