Piotr Łużecki
Piotr Łużecki

Reputation: 1041

Upgrating yourls - shortest time to create new url

I'm thinking how to alter yourls to make new shorten urls faster. I don't really see where it is in the code, but when "yourls" is making new shorten url, it adds title to the database, so I assume that it has to open given url and parses it to get url. I don't need it and maybe it causes that short url creates slowly. I thought about making my own url shortener, but I am really out of time.

What do you think, is it anything that I can change in the yourls script, and where I can find in the code where it's searching it (i would just set title to "" constantly, cause this column can stay in the database for now).

Upvotes: 0

Views: 1082

Answers (2)

Arnold Daniels
Arnold Daniels

Reputation: 16573

If you just want a lightweight URL shortener, writing your own is probably the easiest way to go. Have a look at my super simple shortener script using DBM.

<?php

header('Content-Type: text/plain');

// Add url
if (isset($_GET['add'])) {
    $db = dba_open("/tmp/shortner.db", "c", "db4");
    if (!$db) trigger_error("dba_open failed", E_USER_ERROR);

    $key = substr(base_convert(md5($_GET['add']), 10, 36), 0, 5);
    dba_insert($key, $_GET['add'], $db);
    dba_close($db);

    echo "http://{$_SERVER['HTTP_HOST']}/$key";
    exit();
}

// Get url
$db = dba_open("/tmp/shortner.db", "r", "db4");
if (!$db) trigger_error("dba_open failed", E_USER_ERROR);

if (isset($_GET['key']) && dba_exists($_GET['key'], $db)) {
    $url = dba_fetch($_GET['key'], $db);
}
dba_close($db);

if (!isset($url)) {
    header("HTTP/1.0 404 Not Found");
    echo "Not found $_GET[key]";
    exit();
}

header("Location: $url");
echo $url;

With .htaccess

RewriteEngine On
RewriteRule ^(\w{5})$ /index.php?key=$1

You can easily add a hit counter if you need it, just before doing the direct. (In that case open the DB in 'w' mode).

Note that the key is based on a hash of the URL, so if the same URL is added again it just gets the same key (thus shortened URL).

Upvotes: 1

ethrbunny
ethrbunny

Reputation: 10469

Without actually doing a desc on the query that you are talking about I don't think you can assume that it's the problem. You'd need to profile the whole script to find where it's running slowly.

Upvotes: 0

Related Questions