Code Monkey home page Code Monkey logo

random-string-key-generator's Introduction

Random String Key Generator

This little library makes it easy to generate short, unique, YouTube-like IDs that you can use instead of UUID in your application.

Using shorter (but still unique) ids will have positive inpact on database performance (especially MySQL, which don't handle UUID well).

This library consists of both generator itself, as well as Doctrine integration.

It allows you to customize length of default generator, as well as to write generators of your own.

Will it REALLY generate short unique ids?

Let's look at how many unique values can standard 8 characters long sequence have:

Example id:

9ZGpFadq

8 characters, each can have one of 64 values.

64^8 = 281 474 976 710 656 different values

In the end even UUID might not be unique, it depends on the way you see it. In the end, unless you're using really short ids, it's very close to impossible to get two duplicate strings. I ran multiple tests of generating large amounts of ids (tens of millions) in very short time and couldn't make it to get two duplicate ids.

If you are really afraid of duplicate values, you can extend the key length, but in general this is not neccesary.

1. Installation

Use Composer to install it:

composer require ex3v/random-string-key-generator

2. Usage

Generating ID manually

$factory   = new GeneratorFactory();
$generator = $factory->getGenerator(); //will give you default generator with basic config

$id = $generator->generateId();

Generating ID automatically in Doctrine

Just set up generator annotations (GeneratedValue(strategy="CUSTOM") and CustomIdGenerator(...)) on top of field declaration.

/**
 * @var string
 *
 * @ORM\Id()
 * @ORM\Column(name="id", type="string")
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ex3v\RandomStringKeyGenerator\Doctrine\RandomStringKeyGenerator")
 */
 private $id;

Changing default settings

This library comes with single generator, that returns 8 characters long strings.

Changing default key length and other default settings
//now default generator will return 12 characters long keys
GeneratorFactory::setDefaultStrategy(UniqIdBasedGenerator::class, [GeneratorInterface::KEY_LENGTH => 12]);
Getting instance of your own generator
$factory = new GeneratorFactory();

$generator = $factory->getGenerator(YourGeneratorImplementingGeneratorInterface::class, ['generator' => 'options']);
I want to change default generator config for Doctrine and Symfony. How can I do that?

Unfortunately, Doctrine metadata factory restricts from passing any parameters or container parameters/services to ID Generators. Your only option here is to set GeneratorFactory defaults before app initialization. In case of Symfony, the place you're looking for is Kernel class.

3. Testing

Just run

composer test

in this repository, to run the test suite.

4. Contribution

Have idea on how to develop it? Found a bug and want to fix it? Pull Requests and Issues are welcome :)

random-string-key-generator's People

Contributors

ex3v avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.