aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.BL/DiskCacheManager.cs
blob: ab9f2178e4e0fc2deb3e7ecf6e511b92b5b98563 (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
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
    {
        public class DiskCacheContext : LiteDatabase
        {
            public DiskCacheContext(string connectionString, BsonMapper mapper = null) : base(connectionString, mapper)
            {
            }

            protected override void Dispose(bool disposing)
            {

            }

            public new void Dispose()
            {

            }
        }

        private LiteDatabase _context;

        /// <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 LiteDatabase CreateContext()
        {
            if (_context == null)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath));
                _context = new DiskCacheContext($"Filename={DatabasePath};Password=Twine13579");
            }

            return _context;
        }
    }
}