Asp.net2.0中的缓存机制

 

Asp.net2.0中,缓存机制在原来1.1中的“页面缓存”、“用户控件缓存”的基础上又多了一种:数据源缓存。
新增的DataSource控件(数据源控件)将可以很好的解决了“缓存失效”问题。

这里不谈文件型数据源,主要看看 Sql 数据源。

Asp.net2.0和sqlserver配合进行缓存有2种方式:轮询查询通知
轮询-----------主要是针对SQL (7.0, 2000, 2005);
查询通知-----只针对Sql2005。

1、轮询方式编程步骤:

  • 使用 aspnet_regsql.exe 工具为数据库启用通知。 注:aspnet_regsql工具在后台使用SqlCacheDependencyAdmin类的方法,所以可以直接从ASP.NET页面中使用此类的方法。
       aspnet_regsql.exe -S .  -E  -d databasename -ed
    (-S  数据库实例名,这里的“点”代表本机实例; -E  集成身份验证; -d  数据库名称)
    此步骤只需为每个数据库执行一次。
  • 使用 aspnet_regsql.exe 工具为希望依赖的表启用通知。 
      aspnet_regsql  -S . -E -d databasename -t tablename -et
    (-t  表名)
  •  在应用程序的配置中注册通知。 
    <connectionStrings>
     <add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=Morning2;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    。。。。
    <system.web>
        <caching>
          <sqlCacheDependency enabled="true" pollTime="1000" >
            <databases>
              <add name="databasename" connectionStringName="LocalSqlServer" />  //这里的LocalSqlServer必须在前面声明
            </databases>
          </sqlCacheDependency>
        </caching>
    </system.web>
    轮询时间指定应用程序每隔多长时间检查数据是否已更改。
  • 然后就可以在 OutputCache 指令上使用一个 SQL 依赖项: 
    <%@ OutputCache Duration="999999" SqlDependency="databasename:tablename" VaryByParam="none" %>
    或者依赖项也可以直接在数据源控件上指定: 
    <asp:SqlDataSource EnableCaching="true" CacheDuration="Infinite" SqlCacheDependency="databasename:tablename" ... />

2、查询通知方式编程步骤:

  • 与基于轮询的失效不同,无需在应用程序的配置中注册任何 <sqlCacheDependency>。而且,无需使用 aspnet_regsql.exe 工具进行任何特殊配置。
  • 基于通知的依赖项是使用字符串 CommandNotification 在 OutputCache 指令上 配置的。此值告知 ASP.NET 应为页或数据源控件创建基于通知的 依赖项。
    在页上: 
    <%@ OutputCache Duration="999999" SqlDependency="CommandNotification" VaryByParam="none" %>
    在数据源控件上: 
    <asp:SqlDataSource EnableCaching="true" SqlCacheDependency="CommandNotification" CacheDuration="Infinite" ... />
  • 在首次执行某 SQL 查询之前,必须在应用程序某处调用 System.Data.SqlClient.SqlDependency.Start() 方法。此方法应放在 global.asax 文件的 Application_Start() 事件中。

=============================
使用 Sql Server 2005 查询通知的常见问题是:

  • 查询的 SELECT 语句中必须显式包含列名。使用“SELECT *”将使查询不会在 Sql Server 2005 查询通知中注册。
  • 查询中的表名必须包括所有者名称。例如,如果对 pubs 数据库中的 authors 表发出一个查询,则该查询必须用“dbo.authors” 来引用表。
  • 运行查询的安全标识必须具有在 Sql Server 2005 中注册查询通知的权限。此权限可以使用下面的 T-SQL 命令来授予:GRANT SUBSCRIBE QUERY NOTIFICATIONS TO username。
  • 运行查询的安全标识还必须具有从 Sql Server 2005 发送查询通知的权限。此权限可以使用下面的 T-SQL 命令来授予:GRANT SEND ON SERVICE::SqlQueryNotificationService TO username。

注意: 对支持查询通知的查询语法有许多限制。有关具体约束的列表,请参见 Sql Server 2005 Books Online(《Sql Server 2005 联机丛书》)中的主题“创建用于通知的查询”。另外,如果出现查询未被缓存,而是在每个页请求上都执行的情况,则可能是查询没有遵守 Sql Server 2005 所要求的约束,或是 Sql Server 2005 在尝试设置该查询的通知时产生错误。目前,当尝试在 ASP.NET 中设置缓存依赖项时,这些条件中的任何一个都会导致没有任何提示的错误,其最终结果是缓存依赖项始终失效,并因此使得所有相关联的查询始终会在每个页请求上执行。

附:sql2005联机文档(部分)--全文见上面链接ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.SQLSVR.v9.en/udb9/html/3ba5271c-7efa-4a73-a2c4-b4730df3fdc3.htm

Supported SELECT Statements
Query notifications are supported for SELECT statements that meet the following requirements:

  • The projected columns in the SELECT statement must be explicitly stated, and table names must be qualified with two-part names. Notice that this means that all tables referenced in the statement must be in the same database.
  • The statement may not use the asterisk (*) or table_name.* syntax to specify columns.
  • The statement may not use unnamed columns or duplicate column names.
  • The statement must reference a base table.
  • The projected columns in the SELECT statement may not contain aggregate expressions unless the statement uses a GROUP BY expression. When a GROUP BY expression is provided, the select list may contain the aggregate functions COUNT_BIG() or SUM(). However, SUM() may not be specified for a nullable column. The statement may not specify HAVING, CUBE, or ROLLUP.
  • A projected column in the SELECT statement that is used as a simple expression must not appear more than once.
  • The statement must not include PIVOT or UNPIVOT operators.
  • The statement must not include the INTERSECT or EXCEPT operators.
  • The statement must not reference a view.
  • The statement must not contain any of the following: DISTINCT, COMPUTE or COMPUTE BY, or INTO.
  • The statement must not reference server global variables (@@variable_name).
  • The statement must not reference derived tables, temporary tables, or table variables.
  • The statement must not reference tables or views from other databases or servers.
  • The statement must not contain subqueries, outer joins, or self-joins.
  • The statement must not reference the large object types: text, ntext, and image.
  • The statement must not use the CONTAINS or FREETEXT full-text predicates.
  • The statement must not use rowset functions, including OPENROWSET and OPENQUERY.
  • The statement must not use any of the following aggregate functions: AVG, COUNT(*), MAX, MIN, STDEV, STDEVP, VAR, or VARP.
  • The statement must not use any nondeterministic functions, including ranking and windowing functions.
  • The statement must not contain user-defined aggregates.
  • The statement must not reference system tables or views, including catalog views and dynamic management views.
  • The statement must not include FOR BROWSE information.
  • The statement must not reference a queue.
  • The statement must not contain conditional statements that cannot change and cannot return results (for example, WHERE 1=0).

 

### LlamaIndex 多模态 RAG 实现 LlamaIndex 支持多种数据类型的接入与处理,这使得它成为构建多模态检索增强生成(RAG)系统的理想选择[^1]。为了实现这一目标,LlamaIndex 结合了不同种类的数据连接器、索引机制以及强大的查询引擎。 #### 数据连接器支持多样化输入源 对于多模态数据的支持始于数据收集阶段。LlamaIndex 的数据连接器可以从多个异构资源中提取信息,包括但不限于APIs、PDF文档、SQL数据库等。这意味着无论是文本还是多媒体文件中的内容都可以被纳入到后续的分析流程之中。 #### 统一化的中间表示形式 一旦获取到了原始资料之后,下一步就是创建统一而高效的内部表达方式——即所谓的“中间表示”。这种转换不仅简化了下游任务的操作难度,同时也提高了整个系统的性能表现。尤其当面对复杂场景下的混合型数据集时,良好的设计尤为关键。 #### 查询引擎助力跨媒体理解能力 借助于内置的强大搜索引擎组件,用户可以通过自然语言提问的形式轻松获得所需答案;而对于更复杂的交互需求,则提供了专门定制版聊天机器人服务作为补充选项之一。更重要的是,在这里实现了真正的语义级关联匹配逻辑,从而让计算机具备了一定程度上的‘认知’功能去理解和回应人类意图背后所蕴含的意义所在。 #### 应用实例展示 考虑到实际应用场景的需求多样性,下面给出一段Python代码示例来说明如何利用LlamaIndex搭建一个多模态RAG系统: ```python from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, PromptHelper, ServiceContext from langchain.llms.base import BaseLLM import os def create_multi_modal_rag_system(): documents = SimpleDirectoryReader(input_dir='./data').load_data() llm_predictor = LLMPredictor(llm=BaseLLM()) # 假设已经定义好了具体的大型预训练模型 service_context = ServiceContext.from_defaults( chunk_size_limit=None, prompt_helper=PromptHelper(max_input_size=-1), llm_predictor=llm_predictor ) index = GPTSimpleVectorIndex(documents, service_context=service_context) query_engine = index.as_query_engine(similarity_top_k=2) response = query_engine.query("请描述一下图片里的人物表情特征") print(response) ``` 此段脚本展示了从加载本地目录下各类格式文件开始直到最终完成一次基于相似度排序后的top-k条目返回全过程。值得注意的是,“query”方法接收字符串参数代表使用者想要询问的内容,而在后台则会自动调用相应的解析模块并结合先前准备好的知识库来进行推理计算得出结论。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值