aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.BL/DiskCacheManager.cs
blob: da50d8c1cc23c6f9d5d7f4cf8cefbfda854ec891 (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
104
105
106
107
108
109
110
111
112
113
114
using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DeviceId.Components;
using DeviceId;
using Tango.Core;

namespace Tango.FSE.BL
{
    /// <summary>
    /// Represents a disk cache manager.
    /// </summary>
    public class DiskCacheManager : ExtendedObject, 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", BuildProvider.Build.ToDescription(), 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)
            {
                try
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath));
                    var deviceId = new DeviceIdBuilder().AddMotherboardSerialNumber().ToString();
                    _database = new LiteDatabase($"Filename={DatabasePath};Password={deviceId}");
                }
                catch (Exception ex)
                {
                    throw LogManager.Log(ex, "Error accessing disk cache.");
                }
            }

            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 { }
            }
        }
    }
}