数据库升级PostgreSql+Garnet
  TWYX9ldS9bqI 2024年05月17日 30 0

前言

我公司用的是sql server 2008的破解版,但是现在毕竟是2024年了,打算上最新最强的免费数据库。而且我公司的项目连redis都没用过,我打算测试一下缓存数据库。
网络数据库和缓存数据库了解了一下,打算选择PostgreSql和granet。这两个的特点是,开源,而且性能极强。基本大部分项目都够用了。

PostgreSql

安装

PostgreSql官网地址:https://www.postgresql.org/download/

PostgreSql Windows平台下载地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

菜鸟教程 Windows 上安装 PostgreSQL:https://www.runoob.com/postgresql/windows-install-postgresql.html

安装成功!

不知道为什么我Navicate连接不了

测试

我们这里用的是Freesql进行连接

FreeSql 官网:https://freesql.net/

额外Nuget安装

Person.cs

using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisTest.Models
{
    public class Person
    {
        [Column(IsPrimary = true,IsIdentity =true)]
        public int Id { get; set; }

        public string Name { get; set; }


        public int Age { get; set; }
    }
}

模拟运行

using Bogus;
using FreeSql;
using Newtonsoft.Json;
using RedisTest.Models;
using StackExchange.Redis;

namespace RedisTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IFreeSql freeSql = new FreeSqlBuilder()
                .UseConnectionString(DataType.PostgreSQL, "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456; Database=testDb;ArrayNullabilityMode=Always;Pooling=true;Minimum Pool Size=1")
                .UseAutoSyncStructure(true)//CodeFrist
                .Build();

            if (freeSql.Ado.ExecuteConnectTest())
            {
                Console.WriteLine("数据库连接成功!");
            }
            else
            {
                Console.WriteLine("数据库连接失败");
            }

            //Bogus生成模拟数据
            var faker = new Faker<RedisTest.Models.Person>()
                .RuleFor(t=>t.Name,f=>f.Name.FirstName())
                .RuleFor(t=>t.Age,f=>f.Random.Int(10,30));

            //插入
            {
                var lists = faker.Generate(10);
                freeSql.Insert(lists).ExecuteAffrows();
                Console.WriteLine($"插入数据");
                Console.WriteLine(JsonConvert.SerializeObject(lists, Formatting.Indented));
            }
            //查询最新的10条
            {
                var lists  = freeSql.Select<RedisTest.Models.Person>()
                    .OrderByDescending(t=>t.Id)
                    .Take(10)
                    .ToList();
                Console.WriteLine($"查询数据");
                Console.WriteLine(JsonConvert.SerializeObject(lists, Formatting.Indented));
            }


            Console.WriteLine("运行结束");
            Console.ReadLine();
        }
    }
}

postgresqlAdmin我实在是看不懂,而且我希望是用统一的数据库管理工具去简单查询数据。但是postgresql在升级到15之后,navicate就不兼容了,原因是postgresql 删掉了一个表。

解决方案

实操解决Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错的问题:https://blog.csdn.net/zxp3817100/article/details/134822475

我后面用vscode 去修改16进制的文件的
vs code 以16进制格式查看文件:https://blog.csdn.net/weixin_42533987/article/details/111821190

Garnet

Garnet是微软的开发的一个开源的缓存数据库,类似于C# 版本的Redis。

为什么要选择Garnet而不是Redis

Redis不再开源

Redis不再开源,后续使用3月20日以后的版本需要遵守新的协议。协议详情可以参考:https://github.com/redis/redis?tab=License-1-ov-file#readme

Windows版的Redis是由微软维护的

Redis的Windows版本是微软维护的,因为原作者antirez 只做Linux部分,Windows他不愿意做。就算微软提交了代码他也不愿意合并。

Redis官方为什么不提供Windows版本?:https://www.zhihu.com/question/424272611/answer/2831004593

Windows Redis版本老旧,后续可能不再更新

Redis Github下载地址:https://github.com/tporadowski/redis/releases

Garnet性能强于Redis

Garnet: 力压Redis的C#高性能分布式存储数据库:https://www.cnblogs.com/InCerry/p/18083820/garnet_introduce

安装

开源仓库地址:https://github.com/microsoft/garnet
文档地址:https://microsoft.github.io/garnet/

安装成功

测试

安装可视化工具

免费实用的 Redis 可视化工具推荐, Redis DeskTop Manager 及 Another Redis Desktop Manager 的安装与使用,Redis Insight 下载安装:https://blog.csdn.net/boboJon/article/details/135073969

C# 代码连接测试

官方说,可以直接用redis的连接方式无缝连接garnet。我这里试一下

using Bogus;
using FreeSql;
using Newtonsoft.Json;
using RedisTest.Models;
using StackExchange.Redis;

namespace RedisTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 连接到garnet,这里要写死端口
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:3278");

            // Get the database (by default, it's DB 0)
            IDatabase db = redis.GetDatabase();

            // 存入数据
            db.StringSet("key1", "Hello, Redis!");

            // 读取数据
            string value = db.StringGet("key1");
            Console.WriteLine(value); // Output: Hello, Redis!

            // Increment a value
            db.StringIncrement("counter1");
            long counterValue = (long)db.StringGet("counter1");
            Console.WriteLine(counterValue); // Output: 1

            Console.WriteLine("运行结束");
            Console.ReadLine();
        }
    }
}

总结

这次我就是尝试一下新的技术,毕竟现在的主流还是Mysql+Redis。postgreSql+Garnet用起来还是有点激进了。但是我Garnet是完全按照Redis协议写的,可以无缝转到Garnet上面。而PostgreSql的数据库的性能就是碾压Mysql的,这个是已经得到证实的了。反正我后面的技术选型就往Postgresql和Garnet上面靠

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年05月17日 0

暂无评论

推荐阅读
  NPQODODLqddb   2024年05月17日   63   0   0 .NET
  mVIyUuLhKsxa   2024年05月17日   50   0   0 .NET
  XkHDHG7Y62UM   2024年05月17日   42   0   0 .NET
  f18CFixvrKz8   2024年05月18日   78   0   0 .NET
  rBgzkhl6abbw   2024年05月18日   71   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   39   0   0 .NET
  S34pIcuyyIVd   2024年05月17日   55   0   0 .NET
  gKJ2xtp6I8Y7   2024年05月17日   49   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   32   0   0 .NET
TWYX9ldS9bqI