// Arquivo de exemplo: Cache.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Cache : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataList1.DataSource = OrigemDados();
            DataList1.DataBind();
        }
    }

    DataView OrigemDados()
    {
        DataView origem = null;
        if (Cache["cacheItem"] == null)
        {
            ConnectionStringSettings getString = WebConfigurationManager.ConnectionStrings["nwind"] as ConnectionStringSettings;
            if (getString != null)
            {
                string sSql = "select Products.ProductName,Products.QuantityPerUnit, Products.UnitPrice from Products";
                DataSet ds = new DataSet();

                using (SqlConnection conn = new SqlConnection(getString.ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand(sSql, conn);
                    cmd.CommandType = CommandType.Text;

                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds);

                    origem = new DataView(ds.Tables[0]);
                    Cache.Insert("cacheItem", origem, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
                }
            }
        }
        else
        {
            origem = Cache["cacheItem"] as DataView;
        }
        return origem;
    }
}

