将viewstate存储在数据库中并压缩 附代码(store viewstate in database and compress)

无聊时候写了一个

下图是没有存储viewstate到数据库时

下图是存储到数据库时候,大家可以比较一下性能。

至于页面大小也就不说了,大家都知道如果用一些诸如gridview控件的时候viewstate会相当大的。

web.config里面有两个键值,可以设置来决定是否要存储到数据库或者压缩。

  <add key="StoreViewStateInDB" value="true"/>
  <add key="CompressViewState" value="true"/>

压缩方面还是用的SharpZipLib,一开始用里面的BZip,但是解压用Compress()这个方法老是报错。只能实例化inputstream outputstream解决,参考了codeproject.http://www.codeproject.com/KB/dotnet/mscompression.aspx

附上代码:

/Files/beyondjay/ViewStatePractise.zip

提醒:如果存数据库的话需要有个SQL JOB去删除那些不需要的viewstate,可以根据时间判断,每15分钟或者每1小时等。

Job 的 Script,很简单。

USE [msdb]
GO
/****** Object:  Job [Delete ViewState]    Script Date: 01/07/2009 17:13:59 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [Web Assistant]    Script Date: 01/07/2009 17:13:59 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Web Assistant' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Web Assistant'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Delete ViewState',
  @enabled=1,
  @notify_level_eventlog=0,
  @notify_level_email=0,
  @notify_level_netsend=0,
  @notify_level_page=0,
  @delete_level=0,
  @description=N'Delete ViewState for every 6 hours',
  @category_name=N'Web Assistant',
  @owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Delete ViewState]    Script Date: 01/07/2009 17:13:59 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Delete ViewState',
  @step_id=1,
  @cmdexec_success_code=0,
  @on_success_action=1,
  @on_success_step_id=0,
  @on_fail_action=2,
  @on_fail_step_id=0,
  @retry_attempts=0,
  @retry_interval=0,
  @os_run_priority=0, @subsystem=N'TSQL',
  @command=N'DELETE FROM [State]
WHERE DATEDIFF(mi, [State].[CreateTime], GETDATE()) > 60',
  @database_name=N'AdvancedApplicationDesign',
  @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Delete ViewState',
  @enabled=1,
  @freq_type=4,
  @freq_interval=1,
  @freq_subday_type=4,
  @freq_subday_interval=15,
  @freq_relative_interval=0,
  @freq_recurrence_factor=0,
  @active_start_date=20090107,
  @active_end_date=99991231,
  @active_start_time=0,
  @active_end_time=235959
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:


 

性能测试用具是JetBrains dotTrace 3.1

环境是VS2005, SQL2005

posted @ 2009-01-07 15:22  helloworld22  阅读(825)  评论(3编辑  收藏  举报