Webページキャッシュ技術のLocal Storage
業務:ページのテキストボックスの値に変化があるかどうかを検査して、ある話はキャッシュに保存して、そしてデータベースに保存して、このようにユーザーが異常に操作した後に更にブラウザでホームページを開けて、再びデータを記入することを免れることができます
データベース・テーブル:Test、フィールドを含む:PageName、PageValue
BLL層コード:
Controllerコード:
localstoragehelper.js:
localcache.js:
ページ(mvc 4)コード:
データベース・テーブル:Test、フィールドを含む:PageName、PageValue
BLL層コード:
#region
///
///
///
///
///
public string GetCache(string pageName)
{
if (string.IsNullOrEmpty(pageName)) throw new Exception("pageName is null");
string selectPageValue_sql = "select PageValue from Test where PageName=@PageName";
DataRowCollection rows = SqlHelper.ExecuteTable(selectPageValue_sql, new SqlParameter("@PageName", pageName)).Rows;
if (rows.Count == 0) return string.Empty;
return rows[0][0].ToString();
}
#endregion
#region /
///
/// /
///
///
///
public void TestAE(string pageName, string pageValue)
{
if (string.IsNullOrEmpty(pageName)) throw new Exception("pageName is null");
if (string.IsNullOrEmpty(pageValue)) throw new Exception("pageValue is null");
string selectTest_sql = "select count(1) from Test where PageName=@PageName";
bool isHasThisPageName = (Int32)SqlHelper.ExecuteScalar(selectTest_sql, new SqlParameter("@PageName", pageName)) == 1;
if (isHasThisPageName)//
{
string updateTest_sql = "update Test set PageValue=@PageValue where PageName=@PageName";
SqlHelper.ExecuteNonQuery(updateTest_sql,
new SqlParameter("@PageValue", pageValue),
new SqlParameter("@PageName", pageName));
}
else//
{
string addTest_sql = "insert into Test (PageName,PageValue) values (@PageName,@PageValue)";
SqlHelper.ExecuteNonQuery(addTest_sql,
new SqlParameter("@PageName", pageName),
new SqlParameter("@PageValue", pageValue));
}
}
#endregion
Controllerコード:
///
///
///
public static BLL.BLL_Test b_BLL_Test = new BLL.BLL_Test();
///
///
///
///
public ActionResult LocalCache()
{
return View();
}
///
///
///
///
public JsonResult GetPageCache()
{
try
{
string pageName = Request["PageName"].ToLower();
return Json(b_BLL_Test.GetCache(pageName),"json");
}
catch (Exception ex)
{
return Json(" :" + ex.Message);
}
}
///
/// /
///
///
public ActionResult SetPageCache()
{
try
{
string pageName = Request["PageName"].ToLower();
string pageValue = Request["PageValue"];
b_BLL_Test.TestAE(pageName, pageValue);
return Content(string.Empty);
}
catch (Exception ex)
{
return Content(" :" + ex.Message);
}
}
localstoragehelper.js:
function CheckIsSupport_LocalStorage() {
if (!window.localStorage) {
alert(" Local Storage");
return false;
}
return true;
}
function GetAlong_LocalStorage(key) {
if (!CheckIsSupport_LocalStorage()) return;
return window.localStorage.getItem(key);
}
function GetList_LocalStorage() {
if (!CheckIsSupport_LocalStorage()) return;
var storage = window.localStorage,
list = [];
for (var i = 0; i < storage.length; i++)
list.push("{0}~{1}".format(storage.key(i), storage.getItem(storage.key(i))));
return list;
}
function CreateOrSet_LocalStorage(key, value) {
if (!CheckIsSupport_LocalStorage()) return;
var storage = window.localStorage;
if (storage.getItem(key)) storage.removeItem(key);
storage.setItem(key, value);
}
function Remove_LocalStorage(key) {
if (!CheckIsSupport_LocalStorage()) return;
window.localStorage.removeItem(key);
}
localcache.js:
var pageName = window.location.pathname.toLowerCase(), //
pageDateName = "{0}_date".format(pageName), //
pageCache = GetAlong_LocalStorage(pageName), //
cache_date = GetAlong_LocalStorage(pageDateName); //
$(function () {
//
if (!window.localStorage) {
alert(" Local Storage ");
return;
}
if (!CheckStringIsNull(pageCache)) {//
if (!CheckStringIsNull(cache_date)) {//
if (ComputeDateMin(cache_date) >= 10) {// 10
GetPageNetCacheAndSet(pageName);
} else {// 10
GetPageLocalCache(pageCache);
}
} else {//
GetPageNetCacheAndSet(pageName);
}
} else {//
GetPageNetCacheAndSet(pageName);
}
// ( )
$("input[type=text]").on("change", function () {
var pageValue = [];
$("input[type=text]").each(function (index, item) {
var id = $(item).attr("id"),
val = CheckStringIsNull($(item).val()) ? "" : $(item).val();
pageValue[index] = { "InputID": id, "InputValue": val };
});
if (CheckStringIsNull(pageValue)) return;
// ,
CreateOrSet_LocalStorage(pageName, JSON.stringify(pageValue));
CreateOrSet_LocalStorage(pageDateName, new Date().getTime());
$.post("/Home/SetPageCache", { PageName: pageName, PageValue: JSON.stringify(pageValue) }, function (json) {
if (!CheckStringIsNull(json)) {//
alert(json);
return;
}
}, "text");
});
});
//
function CheckStringIsNull(str) {
return (str == "" || str == null || typeof (str) == undefined);
}
//
function ComputeDateMin(date) {
var minutes = Math.floor((((new Date().getTime() - date) % (24 * 3600 * 1000)) % (3600 * 1000)) / (60 * 1000));
}
// ,
function GetPageNetCacheAndSet() {
$.post("/Home/GetPageCache", { PageName: pageName }, function (json) {
if (json.indexOf(" ") == -1) {
if (!CheckStringIsNull(json)) {
var cache_list = eval('(' + json + ')');
$(cache_list).each(function (index, item) {
$("#{0}".format(cache_list[index].InputID))
.val(cache_list[index].InputValue);
});
CreateOrSet_LocalStorage(pageName, json);
CreateOrSet_LocalStorage(pageDateName, new Date().getTime());//
}
} else {//
alert(json);
}
}, "json");
}
// ,
function GetPageLocalCache(pageCache) {
if (CheckStringIsNull(pageCache)) {// ,
GetPageNetCacheAndSet();
}
var cache_list = eval('(' + pageCache + ')');
$(cache_list).each(function (index, item) {
$("#{0}".format(cache_list[index].InputID))
.val(cache_list[index].InputValue);
});
CreateOrSet_LocalStorage(pageDateName, new Date().getTime());//
}
ページ(mvc 4)コード:
@{
Layout = null;
}
LocalCache
@*
*@
@*
$(function () {
$("input[type=button]").on("click", function () {
var id = $(this).attr("id");
switch (id) {
case "list":
var list = GetList_LocalStorage(),
$con = $("#content");
if (list == "" || list == null || typeof (list) == undefined) {
$con.text("has no local storage.");
return;
}
$con.empty().append("Local storage List:<br/>");
for (var i = 0; i < list.length; i++) {
$con.append("key:{0} value:{1}<hr/>"
.format(list[i].split("~")[0], list[i].split("~")[1]));
}
break;
case "createorset":
CreateOrSet_LocalStorage($("#key").val(), $("#value").val());
$("#list").click();
break;
case "remove":
Remove_LocalStorage($("#removekey").val());
$("#list").click();
break;
default:
}
});
});
*@
午后の成果、もちろんこのようにネットの消耗はとても大きくて、后でまた私に需要を言って、提出ボタンをクリックしてやっとページのデータをキャッシュに保存して、でも少し変えたらいいです.
転載先:https://www.cnblogs.com/myesn/p/5601599.html