last modify尝试

本文介绍了一种使用JavaScript通过AJAX添加请求头来实现304 Not Modified响应的方法,并探讨了前后端如何协作利用HTTP缓存机制提高网页加载效率。

web层的尝试:

javascirpt代码,通过ajax添加请求头,可以达到返回304 not modify,但是需要js自行把数据缓存,否则返回后没有数据展示。

 

//			var date = date_ddmmmyy(new Date(Date.parse(document.lastModified)));
//			var time = document.lastModified.substring(document.lastModified.indexOf(" "));
//			heads ={'Last-Modified':date+time};

 

		//set the last-modify-head
		if(heads){
			if(heads['Last-Modified'])
				this._request.setRequestHeader("If-Modified-Since",heads['Last-Modified']);
		}
		//set the last-modify-hea

 

 

 

//
// format date as dd-mmm-yy
// example: 12-Jan-99
//
function date_ddmmmyy(date)
{
  var d = date.getDate();
  var m = date.getMonth() + 1;
  var y = date.getYear();

  // handle different year values 
  // returned by IE and NS in 
  // the year 2000.
  if(y >= 2000)
  {
    y -= 2000;
  }
  if(y >= 100)
  {
    y -= 100;
  }

  // could use splitString() here 
  // but the following method is 
  // more compatible
  var mmm = 
    ( 1==m)?'Jan':( 2==m)?'Feb':(3==m)?'Mar':
    ( 4==m)?'Apr':( 5==m)?'May':(6==m)?'Jun':
    ( 7==m)?'Jul':( 8==m)?'Aug':(9==m)?'Sep':
    (10==m)?'Oct':(11==m)?'Nov':'Dec';

  return "" +
    (d<10?"0"+d:d) + "-" +
    mmm + "-" +
    (y<10?"0"+y:y);
}

 

后台java处理

org.springframework.web.context.request.WebRequest		

//check is modify
		if(webRequest.checkNotModified(lastModifyTime)){
			return null;
		}

 

 

 

 

void cleanup_expired_nonces() { pthread_mutex_lock(&g_nonce_store.lock); time_t current_time = time(NULL); /* 检查是否需要清理 */ if (current_time - g_nonce_store.lastmodify > CLEANUP_INTERVAL) { ​ HTTPD_ERROR("执行清理:上次修改 %.f 秒前\n", ​ difftime(current_time, g_nonce_store.lastmodify)); ​ ​ /* 清空数组 */ ​ memset(g_nonce_store.nonces, 0, sizeof(g_nonce_store.nonces)); ​ g_nonce_store.count = 0; ​ ​ /* 更新最后修改时间 */ ​ g_nonce_store.lastmodify = current_time; } pthread_mutex_unlock(&g_nonce_store.lock); } /* 添加 nonce 到全局存储 */ void add_nonce(const char* nonce) { if (!nonce || g_nonce_store.count >= MAX_NONCES) return; pthread_mutex_lock(&g_nonce_store.lock); strncpy(g_nonce_store.nonces[g_nonce_store.count], ​ nonce, LEN_DIGEST_NONCE); g_nonce_store.count++; g_nonce_store.lastmodify = time(NULL); pthread_mutex_unlock(&g_nonce_store.lock); } 客户端发来http请求(含Authorization头)》判断在nonce集合中是否存在:不存在返回401 Unauthorized,存在则验证标志位》验证标志位为1返回401 Unauthorized,标志位为0则置为1。 为了避免数组过大,设置为60个,需要每隔一段时间清一次nonce。在生成nonce时,记录该nonce的生成时间,还有是否已经被认证使用过。在整个nonce数组中需要记录一个count和一个cleantime。设置cleantime为过期时间,当最早生成的nonce已经过了过期时间,则清除该nonce,数组指针往后移动一个nonce长度,到达数组的最后之后,再从头开始填充。第二种情况是数组满了则进行清理,剔除最早生成的nonce(即当前指针所在的那一个),数组指针往后移动一个nonce长度,更新cleantime为当前剔除的最早生成的nonce距离现在的时间。——能不能根据新的生成和清理机制对我的代码进行修改
11-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值