• EnterpriseLibrary+Oralce实现缓存失效并加载数据


    本文主要介绍一个简单的解决方案。

    主要用到了Caching Application Block+Data Access Application Block+Oracle

    原理介绍:给表增加一个触发器,在修改数据时,调用Java存储过程修改一个外部txt文件,让缓存失效,然后重新加载数据。

    增加一个测试表wf_sms,表结构如下:

    Java存储过程代码:

    create or replace and compile java source named say as
    import java.io.*;
    public class Hello
    {
    public static void say(String str)
    {
    try {
    File aa
    =new File("C:\\aaa.txt");
    aa.delete();
    aa.createNewFile();
    FileWriter bb
    =new FileWriter(aa);
    bb.write(str);
    bb.flush();
    bb.close();
    }
    catch(IOException e)
    {

    }
    }
    }

    增加一个函数调用用Java存储过程:

    create or replace function hello(name varchar2) return varchar2
    as language java name 'Hello.say(java.lang.String) return java.lang.String';

    增加一个触发器:

    CREATE OR REPLACE TRIGGER tr_wf_sms
    BEFORE
    INSERT OR DELETE OR UPDATE ON wf_sms
    FOR EACH ROW

    DECLARE
    abc
    VARCHAR(200);
    BEGIN

    SELECT hello(:NEW.Test2) INTO abc FROM dual;
    --execute immediate hello(:NEW.Test2);
    END;

    数据库方面的准备工作做完了,现在新建一个c/s程序,界面如下

    详细代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Practices.EnterpriseLibrary.Caching;
    using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System.Data.Common;

    namespace EnterpriseLibraryTest1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    ICacheManager cacheManager;
    Database db
    = DatabaseFactory.CreateDatabase();
    DataTable _table;

    /// <summary>
    /// 加载缓存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
    FileDependency _fileDep
    = new FileDependency("c:\\aaa.txt");
    cacheManager.Add(_table.Rows[
    0]["test1"].ToString(), _table.Rows[0]["test2"].ToString(), CacheItemPriority.Normal, null, _fileDep);//加入缓存
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    cacheManager
    = CacheFactory.GetCacheManager();//实例化ICachemanager
    string sql = "select * from wf_sms";
    DbCommand dc
    = db.GetSqlStringCommand(sql);
    _table
    = db.ExecuteDataSet(dc).Tables[0];
    }
    /// <summary>
    /// 获取缓存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
    if (cacheManager.GetData("aaa") == null)
    {
    FileDependency _fileDep1
    = new FileDependency("c:\\aaa.txt");
    cacheManager.Remove(
    "aaa");
    string sql = "select test2 from wf_sms where test1='aaa'";
    DbCommand dc
    = db.GetSqlStringCommand(sql);
    string str = (string)db.ExecuteScalar(dc);
    cacheManager.Add(
    "aaa", str, CacheItemPriority.Normal, null, _fileDep1);
    }
    this.richTextBox1.Text = cacheManager.GetData("aaa").ToString();
    }
    /// <summary>
    /// 修改数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, EventArgs e)
    {
    string sql = "update wf_sms set test2='" + Guid.NewGuid().ToString() + "' where test1='aaa'";
    db.ExecuteNonQuery(CommandType.Text, sql);
    }
    }
    }
  • 相关阅读:
    C# 执行bat文件 PHP
    windows服务操作 sc批处理 PHP
    HTML 彩虹 PHP
    C# 简易日志记录类 PHP
    C# 读写INI文件 PHP
    .NET Framework PHP
    序列号备忘 PHP
    获取浏览器版本信息
    数据库中Image字段存储读取数据
    [转]装机推荐 5000元铸造最强游戏平台
  • 原文地址:https://www.cnblogs.com/xiangboren/p/2107032.html
Copyright © 2020-2023  润新知