root/lang/vb2005/FuwaKumo/trunk/FuwaKumo/CacheList.vb @ 34124

Revision 34124, 3.2 kB (checked in by kiri_feather, 5 years ago)
Line 
1Public Class ImageCache(Of TKey, TValue As CacheItem(Of System.Drawing.Bitmap))
2    Inherits Dictionary(Of TKey, TValue)
3
4    Private _lifetimeHours As Integer
5    Private _size As Integer
6
7    Public Sub New()
8
9    End Sub
10
11    Public Sub New(ByVal lifetimeHours As Integer)
12        _lifetimeHours = lifetimeHours
13        _size = 20
14    End Sub
15
16    Public Shadows Sub Add(ByVal key As TKey, ByVal value As TValue)
17        MyBase.Add(key, value)
18        Truncate()
19    End Sub
20
21    Public Shadows Property Item(ByVal key As TKey) As CacheItem(Of System.Drawing.Bitmap)
22        Get
23            Dim cacheItem As CacheItem(Of System.Drawing.Bitmap) = MyBase.Item(key)
24            cacheItem.CreatedAt = Now
25            Return cacheItem
26        End Get
27        Set(ByVal value As CacheItem(Of System.Drawing.Bitmap))
28            MyBase.Item(key) = value
29        End Set
30    End Property
31
32    Public Property LifetimeHours() As Integer
33        Get
34            If _lifeTimeHours = Integer.MaxValue Then
35                Return 0
36            Else
37                Return _lifetimeHours
38            End If
39        End Get
40        Set(ByVal value As Integer)
41            If value = 0 Then value = Integer.MaxValue
42            _lifetimeHours = value
43            Truncate()
44        End Set
45    End Property
46
47    Private Sub Truncate()
48        Dim keyList As New List(Of TKey)
49        Dim lifeTime As DateTime = Now.AddMinutes(-5)
50        For Each item As KeyValuePair(Of TKey, TValue) In Me
51            If item.Value.CreatedAt.CompareTo(lifeTime) < 0 Then
52                keyList.Add(item.Key)
53            End If
54        Next
55
56        For Each key As TKey In keyList
57            If Me.Item(key) IsNot Nothing Then
58                Me.Item(key).Dispose()
59                Me.Item(key) = Nothing
60            End If
61            Me.Remove(key)
62        Next
63    End Sub
64
65End Class
66
67Public Class CacheItem(Of T As IDisposable)
68    Implements IDisposable
69
70    Public CreatedAt As DateTime
71    Private _value As T
72    Public Sub New(ByVal Value As T)
73        _value = Value
74        CreatedAt = Now
75    End Sub
76
77    Public ReadOnly Property ItemData() As T
78        Get
79            CreatedAt = Now
80            Return _value
81        End Get
82    End Property
83
84    Private disposedValue As Boolean = False        ' �d�������яo����o������
85
86    ' IDisposable
87    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
88        If Not Me.disposedValue Then
89            If disposing Then
90                ' TODO: �����I�ɌĂяo���ꂽ�Ƃ��Ƀ}�l�[�W ���\�[�X�����
91            End If
92
93            ' TODO: ���L�̃A���}�l�[�W ���\�[�X�����
94            If _value IsNot Nothing Then _value.Dispose()
95        End If
96        Me.disposedValue = True
97    End Sub
98
99#Region " IDisposable Support "
100    ' ���̃R�[�h�́A�j��ȃp�^�[���𐳂�������ł�������Visual Basic �ɂ��Ēlj���������B
101    Public Sub Dispose() Implements IDisposable.Dispose
102        ' ���̃R�[�h��X���Ȃ��ł��������B�N���[���A�b�v �R�[�h���Dispose(ByVal disposing As Boolean) �ɋL�q���܂��B
103        Dispose(True)
104        GC.SuppressFinalize(Me)
105    End Sub
106#End Region
107
108End Class
Note: See TracBrowser for help on using the browser.