Efficient string generation and recycling. Help pls

Discussion in 'Programming' started by komirad, Feb 28, 2007.

Thread Status:
Not open for further replies.
  1. #1
    Hi, I am programming in php, but that doesn't matter.
    I need to be able to generate a one character or more string.
    eg. "x5e5" to be put into a url like "mysite.com/x5e5"
    after a period of inactivity i would like to delete it and reuse the string combination.

    How should I generate a string without using a string that is in use?
    Increment works, but it doesn't recycle the string.

    HELP ME. thanks.
     
    komirad, Feb 28, 2007 IP
  2. Forrest

    Forrest Peon

    Messages:
    500
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use a table with a unique index on the string, the date it was created, and the date it was last used. You can check the status of any string with a select query, and you can generate a random string very easily.
     
    Forrest, Mar 1, 2007 IP
  3. komirad

    komirad Well-Known Member

    Messages:
    921
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    108
    #3
    If I generate a random string, it might generate a used string.
    I would have to keep generating random strings indefinitely to get an unused string?
     
    komirad, Mar 1, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use incrementation and add recycling to it.

    The first few strings will be generated in alphanumeric increments. Setup a CRON job that runs regulary and have the script check through all strings and make note of any expired ones. Now when you need to get a new string, it first checks your store of expired strings and takes one from there if possible. If there aren't any, it generates one using an incrementation of the last string.

    That's just one way, there may be a better way but I can't think of anything atm.
     
    rodney88, Mar 2, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Alternatively, you could always just set a table up with a heap of possible strings already defined, then assign URLs to those strings as you want. When you want to undefine a URL, just remove it but keep the string 'key' in the database. When you want to find the 'next' available string, a quick SQL query of something like 'select id from table where not assigned limit 1' will give you that 'next' unused key.

    As the need arises, just add more keys (this could be done automatically almost).
     
    TwistMyArm, Mar 2, 2007 IP
Thread Status:
Not open for further replies.