blob: ece936dee0233eb8f94e0008912eeda29bff69f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.FSE.BL
{
/// <summary>
/// Represents a disk cache manager.
/// </summary>
public class DiskCacheManager : IDisposable
{
public class DiskCacheContext : IDisposable
{
public LiteDatabase Database { get; private set; }
public DiskCacheContext(LiteDatabase database)
{
Database = database;
}
public void Dispose()
{
//Do Nothing.
}
}
private LiteDatabase _database;
/// <summary>
/// Gets the database file path.
/// </summary>
public String DatabasePath { get; private set; }
private static DiskCacheManager _default;
/// <summary>
/// Gets the default instance.
/// </summary>
public static DiskCacheManager Default
{
get
{
if (_default == null)
{
_default = new DiskCacheManager(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Cache", "Tango FSE", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".cache"));
}
return _default;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DiskCacheManager"/> class.
/// </summary>
/// <param name="databasePath">The database path.</param>
public DiskCacheManager(String databasePath)
{
DatabasePath = databasePath;
}
/// <summary>
/// Creates a new disk cache context.
/// </summary>
/// <returns></returns>
public DiskCacheContext CreateContext()
{
if (_database == null)
{
Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath));
_database = new LiteDatabase($"Filename={DatabasePath};Password=Twine13579");
}
return new DiskCacheContext(_database);
}
/// <summary>
/// Finalizes an instance of the <see cref="DiskCacheManager"/> class.
/// </summary>
~DiskCacheManager()
{
Dispose();
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
if (_database != null)
{
try
{
_database.Dispose();
_database = null;
}
catch { }
}
}
}
}
|