DataService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using IMCS.CCS.Common;
  2. using IMCS.CCS.Common.Redis;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. namespace IMCS.CCS.Service.Impl
  10. {
  11. /// <summary>
  12. /// 数据服务
  13. /// </summary>
  14. public class DataService : IDataService
  15. {
  16. private readonly string _webRootPath;
  17. private string PathOrKey { get; set; }
  18. private readonly IDataStorageConfigurationService _dataStorageConfigurationService;
  19. private readonly IRedisService _redisService;
  20. public DataService(IWebHostEnvironment webHostEnvironment, IDataStorageConfigurationService dataStorageConfigurationService, IRedisService redisService)
  21. {
  22. _webRootPath = webHostEnvironment.WebRootPath;
  23. _dataStorageConfigurationService = dataStorageConfigurationService;
  24. _redisService = redisService;
  25. }
  26. public bool Init(string pathOrKey)
  27. {
  28. PathOrKey = pathOrKey;
  29. return true;
  30. }
  31. /// <summary>
  32. /// 读取数据
  33. /// </summary>
  34. /// <typeparam name="T"></typeparam>
  35. /// <returns></returns>
  36. public async Task<IEnumerable<T>> ReadDataAsync<T>()
  37. {
  38. var data = string.Empty;
  39. if (_dataStorageConfigurationService.IsUseRedis())
  40. {
  41. data = await this._redisService.Database.StringGetAsync(PathOrKey);
  42. }
  43. else
  44. {
  45. try
  46. {
  47. var path = this.GetPath();
  48. //using var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  49. //var buffer = new byte[fs.Length];
  50. //fs.Position = 0;
  51. //await fs.ReadAsync(buffer, 0, buffer.Length);
  52. //var data = Encoding.UTF8.GetString(buffer);
  53. //if (string.IsNullOrWhiteSpace(data)) return default;
  54. data = await File.ReadAllTextAsync(path);
  55. }
  56. catch (Exception)
  57. {
  58. return await ReadDataAsync<T>();
  59. }
  60. }
  61. return string.IsNullOrWhiteSpace(data) ? default : JsonConvert.DeserializeObject<IEnumerable<T>>(data);
  62. }
  63. /// <summary>
  64. /// 写入数据
  65. /// </summary>
  66. /// <returns></returns>
  67. public async Task<bool> WriteDataAsync<T>(T contents)
  68. {
  69. var json = JsonConvert.SerializeObject(contents);
  70. if (_dataStorageConfigurationService.IsUseRedis())
  71. {
  72. await this._redisService.Database.StringSetAsync(PathOrKey, json);
  73. }
  74. else
  75. {
  76. try
  77. {
  78. var path = this.GetPath();
  79. //using var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  80. //fs.Seek(0, SeekOrigin.Begin);
  81. //fs.SetLength(0);
  82. //var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(contents));
  83. //fs.Position = 0;
  84. //await fs.WriteAsync(data, 0, data.Length);
  85. await File.WriteAllTextAsync(path, json);
  86. }
  87. catch (Exception)
  88. {
  89. return await WriteDataAsync(contents);
  90. }
  91. }
  92. return true;
  93. }
  94. #region 私有
  95. private string GetPath()
  96. {
  97. if (string.IsNullOrWhiteSpace(PathOrKey)) throw new MessageBox("属性 FilePath 空对象,请使用前调用 Init 函数!");
  98. var path = _webRootPath + PathOrKey;
  99. if (!File.Exists(path))
  100. {
  101. File.Create(path);
  102. }
  103. return path;
  104. }
  105. #endregion
  106. }
  107. }