绝对到期和滑动到期不起作用(缓存)

时间:2018-07-03 14:13:56

标签: asp.net

//伙计们,请帮助绝对缓存和滑动缓存无法正常工作,我无法找到问题所在

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.Caching;

namespace WebApplication154
{
public partial class WebForm1 : System.Web.UI.Page
{
    string cs = 
WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;

代码以在页面加载时绑定网格视图

    protected void Page_Load(object sender, EventArgs e)
    {
        lblserver.Text = DateTime.Now.ToString();
        DataSet ds = getdata();
        Cache["products"] = ds;
        grid1.DataSource = ds;
        grid1.DataBind();

    }

我正在将数据从数据库传递到数据集

    public DataSet getdata()
    {
        SqlConnection conn = new SqlConnection(cs);
        SqlDataAdapter da = new SqlDataAdapter("spfrag", conn);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

即使我提到了以下代码,数据也不会缓存

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (Cache["products"] != null)
        {
            DataSet ds = (DataSet)Cache["products"];
            grid1.DataSource = ds;
            grid1.DataBind();
        }
        else
        {
            DataSet ds = getdata();
            Cache.Add("products", ds, null, Cache.NoAbsoluteExpiration, 
            TimeSpan.FromSeconds(20), CacheItemPriority.Default, null);
            grid1.DataSource = ds;
            grid1.DataBind();
        }
    }


}

1 个答案:

答案 0 :(得分:1)

您可能想尝试从import java.util.*; import java.util.stream.*; public class IntegerSetProcess { public static void main(String[] args) { // Input data List<IntegerSet> inputList = Arrays.asList(new IntegerSet(new int [] {11}), new IntegerSet(new int [] {12, 555}), new IntegerSet(new int [] {2, 333, 555, 9, 144, 89}), new IntegerSet(new int [] {12}), new IntegerSet(new int [] {12, 3, 555, 90, 42, 789, 15000}), new IntegerSet(new int [] {2, 555, 9, 89, 333, 144}), new IntegerSet(new int [] {555, 12}), new IntegerSet(new int [] {222, 12, 41320, 0, 769942}), new IntegerSet(new int [] {910, 77})); // Distinct IntegerSets List<IntegerSet> distinctList = inputList.stream() .distinct() .sorted() .collect(Collectors.toList()); // Filter subsets to get result List<IntegerSet> resultList = doSubsetFiltering(distinctList); // Result data in original form (optional) resultList.stream() .forEach(e -> System.out.println(Arrays.toString(e.getOriginal()))); } /* * Takes the input List<IntegerSet> and removes all the IntegerSets with * elements as subset in any other IntegerSet. */ private static List<IntegerSet> doSubsetFiltering(List<IntegerSet> listIs) { List<IntegerSet> removedIs = new ArrayList<>(); OUTER_LOOP: // size-1, the last element is not iterated for (int i = 0; i < listIs.size()-1; i++) { IntegerSet thisIs = listIs.get(i); INNER_LOOP: // i+1, the checking starts from the next IntegerSet for (int j = i+1; j < listIs.size(); j++) { IntegerSet nextIs = listIs.get(j); if (isSubset(thisIs.getData(), nextIs.getData())) { // To remove thisIs set as it is a subset of isNext removedIs.add(thisIs); break INNER_LOOP; } } // inner-for-loop } // outer for-loop listIs.removeAll(removedIs); return listIs; } // Returns true if the input array thisIs has all its elements in nextIs. public static boolean isSubset(int[] thisIs, int[] nextIs) { for(int i : thisIs) { if (Arrays.binarySearch(nextIs, i) < 0) { return false; } } return true; } } import java.util.*; import java.util.stream.*; public class IntegerSet implements Comparable<IntegerSet> { private int[] data; private int[] original; public IntegerSet(int[] intput) { original = IntStream.of(intput).toArray(); data = intput; Arrays.sort(data); } public int[] getData() { return data; } public int[] getOriginal() { return original; } @Override public String toString() { return Arrays.toString(data); } @Override public boolean equals(Object obj) { IntegerSet is = (IntegerSet) obj; if (Arrays.equals(data, is.getData())) { return true; } return false; } @Override public int hashCode() { return data.length; } @Override public int compareTo(IntegerSet is) { return Integer.valueOf(data.length).compareTo(is.getData().length); } } 中的缓存中拉出,并且仅在其中缓存为空时才从数据库中拉出。查看example

的缓存存储库模式

现在这种编码方式只能在第二次getdata()事件触发时从缓存中提取。您最终也将在每个btnsubmit_Click上访问数据库,这也会在Page_Load上触发。

相关问题